committed by
Sacha Al Himdani
parent
288c59a5f2
commit
f9216d30b2
@@ -30,9 +30,7 @@ func Unauthorized() *gqlerror.Error {
|
||||
}
|
||||
|
||||
func AuthenticationRequired(details map[string]any) *gqlerror.Error {
|
||||
extensions := map[string]any{
|
||||
"code": "AUTHENTICATION_REQUIRED",
|
||||
}
|
||||
extensions := map[string]any{"code": "AUTHENTICATION_REQUIRED"}
|
||||
maps.Copy(extensions, details)
|
||||
|
||||
return &gqlerror.Error{
|
||||
@@ -59,11 +57,14 @@ func Conflict(err error) *gqlerror.Error {
|
||||
}
|
||||
}
|
||||
|
||||
func Invalid(err error) *gqlerror.Error {
|
||||
func Invalid(err error, details map[string]any) *gqlerror.Error {
|
||||
extensions := map[string]any{"code": "INVALID_REQUEST"}
|
||||
if details != nil {
|
||||
maps.Copy(extensions, details)
|
||||
}
|
||||
|
||||
return &gqlerror.Error{
|
||||
Message: err.Error(),
|
||||
Extensions: map[string]any{
|
||||
"code": "INVALID",
|
||||
},
|
||||
Message: err.Error(),
|
||||
Extensions: extensions,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"go.gearno.de/kit/log"
|
||||
"go.probo.inc/probo/pkg/auth"
|
||||
"go.probo.inc/probo/pkg/authz"
|
||||
"go.probo.inc/probo/pkg/validator"
|
||||
)
|
||||
|
||||
func RecoverFunc(ctx context.Context, err any) error {
|
||||
@@ -50,6 +51,27 @@ func RecoverFunc(ctx context.Context, err any) error {
|
||||
})
|
||||
}
|
||||
|
||||
var errValidations validator.ValidationErrors
|
||||
if errors.As(asError(err), &errValidations) {
|
||||
gqlErrors := gqlerror.List{}
|
||||
|
||||
for _, err := range errValidations {
|
||||
gqlErrors = append(
|
||||
gqlErrors,
|
||||
Invalid(
|
||||
err,
|
||||
map[string]any{
|
||||
"cause": err.Code,
|
||||
"field": err.Field,
|
||||
"value": err.Value,
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return gqlErrors
|
||||
}
|
||||
|
||||
var tenantAccessErr *authz.TenantAccessError
|
||||
if errTyped, ok := err.(error); ok && errors.As(errTyped, &tenantAccessErr) {
|
||||
return Unauthorized()
|
||||
@@ -65,5 +87,6 @@ func asError(err any) error {
|
||||
if e, ok := err.(error); ok {
|
||||
return e
|
||||
}
|
||||
|
||||
return errors.New("unknown panic")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user