package apperror
import (
"context"
detailtype "github.com/yunerou/aerro/aerror/detail_type"
"git.ponos-tech.com/gophers/toys/providers/validation"
"github.com/samber/lo"
)
func ValidationErrsAggregate(
ctx context.Context,
validationErrs []validation.ValidationErr,
) DetailAppError {
detail := detailtype.ValidateError(
lo.Map(
validationErrs,
func(vErr validation.ValidationErr, _ int) detailtype.ValidateErrorItem {
return detailtype.ValidateErrorItem{
Key: vErr.Field,
Message: vErr.Error(),
}
},
))
return NewWithDetail(ctx, ErrManyValidation, nil, detail, map[string]any{
"Length": len(validationErrs),
})
}
// get http status code which corresponds to error.
func (e *appError) HTTPStatusCode() int {
x := int(e.errCode)
switch {
case x == int(ErrOrigin):
return http.StatusBadRequest
case x > int(start400) && x < int(end400):
return http.StatusBadRequest
case x > int(start401) && x < int(end401):
return http.StatusUnauthorized
case x > int(start403) && x < int(end403):
return http.StatusForbidden
case x > int(start500) && x < int(end500):
return http.StatusInternalServerError
case x > int(start500Trace) && x < int(end500Trace):
return http.StatusInternalServerError
}
return http.StatusNotExtended
}