Added errors for services
This commit is contained in:
parent
ab4b53fd40
commit
c3c3d65d32
12 changed files with 217 additions and 86 deletions
30
internal/utils/error_handler.go
Normal file
30
internal/utils/error_handler.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
ie "58team_blog/internal/application/errors"
|
||||
"58team_blog/internal/interfaces/api/responses"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func HandleError(err error) responses.ErrorResponse {
|
||||
var errorCode int
|
||||
errorMsg := err.Error()
|
||||
|
||||
log.Println(err)
|
||||
|
||||
if errors.Is(&ie.ValidationError{}, err) {
|
||||
errorCode = http.StatusBadRequest
|
||||
} else if errors.Is(&ie.NotFoundError{}, err) {
|
||||
errorCode = http.StatusNotFound
|
||||
} else if errors.Is(&ie.AlreadyExistsError{}, err) {
|
||||
errorCode = http.StatusConflict
|
||||
} else if errors.Is(&ie.DBError{}, err) {
|
||||
errorCode = http.StatusInternalServerError
|
||||
} else {
|
||||
errorCode = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
return responses.CreateErrorResponse(errorCode, errorMsg)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue