2023-12-21 16:04:04 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:03:25 +00:00
|
|
|
resErr := newServiceError(illegalArgument, fmt.Errorf("validation error: %v (%w)", util.ValuesString(errorMap), err))
|
2023-12-21 16:04:04 +00:00
|
|
|
c.Header(headerContentType, headerContentTypeApplicationJson)
|
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, resErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func errToHttpStatus(err error) int {
|
|
|
|
var e *serviceError
|
|
|
|
switch {
|
|
|
|
case errors.As(err, &e):
|
2024-06-10 18:03:25 +00:00
|
|
|
if e.Status == illegalArgument {
|
2023-12-21 16:04:04 +00:00
|
|
|
return http.StatusBadRequest
|
2024-06-10 18:03:25 +00:00
|
|
|
} else if e.Status == unauthorized {
|
2023-12-21 16:04:04 +00:00
|
|
|
return http.StatusUnauthorized
|
2024-06-10 18:03:25 +00:00
|
|
|
} else if e.Status == forbidden {
|
2023-12-21 16:04:04 +00:00
|
|
|
return http.StatusForbidden
|
2024-06-10 18:03:25 +00:00
|
|
|
} else if e.Status == notFound {
|
2023-12-21 16:04:04 +00:00
|
|
|
return http.StatusNotFound
|
2024-06-10 18:03:25 +00:00
|
|
|
} else if e.Status == methodNotAllowed {
|
|
|
|
return http.StatusMethodNotAllowed
|
|
|
|
} else if e.Status == conflict {
|
2023-12-21 16:04:04 +00:00
|
|
|
return http.StatusConflict
|
2024-06-10 18:03:25 +00:00
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:03:25 +00:00
|
|
|
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())
|
|
|
|
}
|