upda/server/api_handler_error.go
Varakh 1fc3818d3c
All checks were successful
/ build (push) Successful in 5m3s
feature(api,release): prepare for next major release and switch to requiring content-type set to JSON for all incoming requests and expose more CORS environment variables
- Switched to enforce JSON as Content-Type for all incoming requests
- Switched to properly respond with JSON on page not found or method not allowed
- Renamed CORS_ALLOW_ORIGIN to CORS_ALLOW_ORIGINS
- Added CORS_ALLOW_CREDENTIALS which defaults to true
- Added CORS_EXPOSE_HEADERS which defaults to *
- Overhauled package visibility for server module
2024-06-10 20:03:25 +02:00

79 lines
2 KiB
Go

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
}
resErr := newServiceError(illegalArgument, fmt.Errorf("validation error: %v (%w)", util.ValuesString(errorMap), err))
c.Header(headerContentType, headerContentTypeApplicationJson)
_ = c.AbortWithError(http.StatusBadRequest, resErr)
return
}
func errToHttpStatus(err error) int {
var e *serviceError
switch {
case errors.As(err, &e):
if e.Status == illegalArgument {
return http.StatusBadRequest
} else if e.Status == unauthorized {
return http.StatusUnauthorized
} else if e.Status == forbidden {
return http.StatusForbidden
} else if e.Status == notFound {
return http.StatusNotFound
} else if e.Status == methodNotAllowed {
return http.StatusMethodNotAllowed
} else if e.Status == conflict {
return http.StatusConflict
} else if e.Status == general {
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)
}
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())
}