Backend/internal/utils/error_handler.go

39 lines
952 B
Go

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)
var validationErr *ie.ValidationError
var readFileErr *ie.ReadFileError
var notFoundErr *ie.NotFoundError
var alreadyExistsErr *ie.AlreadyExistsError
var dbErr *ie.DBError
switch {
case errors.As(err, &validationErr):
errorCode = http.StatusBadRequest
case errors.As(err, &readFileErr):
errorCode = http.StatusInternalServerError
case errors.As(err, &notFoundErr):
errorCode = http.StatusNotFound
case errors.As(err, &alreadyExistsErr):
errorCode = http.StatusConflict
case errors.As(err, &dbErr):
errorCode = http.StatusInternalServerError
default:
errorCode = http.StatusInternalServerError
}
return responses.CreateErrorResponse(errorCode, errorMsg)
}