upda/server/api_handler_error.go

81 lines
2 KiB
Go
Raw Normal View History

2023-12-21 16:04:04 +00:00
package server
import (
"errors"
"fmt"
"git.myservermanager.com/varakh/upda/api"
2023-12-21 16:04:04 +00:00
"git.myservermanager.com/varakh/upda/util"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"net/http"
)
func errAbortWithValidatorPayload(c *gin.Context, err error) {
var errs validator.ValidationErrors
errors.As(err, &errs)
errorMap := make(map[string]string)
for _, v := range errs {
key, txt := validatorErrorToText(&v)
errorMap[key] = txt
}
resErr := newServiceError(illegalArgument, fmt.Errorf("validation error: %v (%w)", util.ValuesString(errorMap), err))
c.Header(api.HeaderContentType, api.HeaderContentTypeApplicationJson)
2023-12-21 16:04:04 +00:00
_ = c.AbortWithError(http.StatusBadRequest, resErr)
return
}
func errToHttpStatus(err error) int {
var e *serviceError
switch {
case errors.As(err, &e):
if e.Status == illegalArgument {
2023-12-21 16:04:04 +00:00
return http.StatusBadRequest
} else if e.Status == unauthorized {
2023-12-21 16:04:04 +00:00
return http.StatusUnauthorized
} else if e.Status == forbidden {
2023-12-21 16:04:04 +00:00
return http.StatusForbidden
} else if e.Status == notFound {
2023-12-21 16:04:04 +00:00
return http.StatusNotFound
} else if e.Status == methodNotAllowed {
return http.StatusMethodNotAllowed
} else if e.Status == conflict {
2023-12-21 16:04:04 +00:00
return http.StatusConflict
} else if e.Status == general {
2023-12-21 16:04:04 +00:00
return http.StatusInternalServerError
}
default:
return http.StatusInternalServerError
}
return -1
}
func errCodeToStr(err error) string {
var e *serviceError
ok := errors.As(err, &e)
if ok {
return string(e.Status)
}
return string(general)
2023-12-21 16:04:04 +00:00
}
func validatorErrorToText(e *validator.FieldError) (string, string) {
x := *e
switch x.Tag() {
case "required":
return x.Field(), fmt.Sprintf("%s is required", x.Field())
case "max":
return x.Field(), fmt.Sprintf("%s cannot be longer than %s", x.Field(), x.Param())
case "min":
return x.Field(), fmt.Sprintf("%s must be longer than %s", x.Field(), x.Param())
case "len":
return x.Field(), fmt.Sprintf("%s must be %s characters long", x.Field(), x.Param())
}
return x.Field(), fmt.Sprintf("%s is not valid", x.Field())
}