Skip to content

Commit

Permalink
Improve error handling for validation in GraphQL
Browse files Browse the repository at this point in the history
- Add handling for DestArgError and ActionParamError
- Include client-specific error details in responses
  • Loading branch information
mastercactapus committed Sep 4, 2024
1 parent c82aa1a commit d99a530
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions graphql2/graphqlapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,31 @@ func (a *App) Handler() http.Handler {
},
}
}

var argErr *nfydest.DestArgError
if errors.As(err, &argErr) {
return &gqlerror.Error{
Message: argErr.Err.Error(),
Path: graphql.GetPath(ctx),
Extensions: map[string]interface{}{
"code": graphql2.ErrorCodeInvalidDestFieldValue,
"fieldID": argErr.FieldID,
},
}
}

var paramErr *nfydest.ActionParamError
if errors.As(err, &paramErr) {
return &gqlerror.Error{
Message: paramErr.Err.Error(),
Path: graphql.GetPath(ctx),
Extensions: map[string]interface{}{
"code": graphql2.ErrorCodeInvalidMapFieldValue,
"key": paramErr.ParamID,
},
}
}

err = errutil.MapDBError(err)
var gqlErr *gqlerror.Error

Expand Down
2 changes: 2 additions & 0 deletions notification/nfydest/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type DestArgError struct {

func (e *DestArgError) Error() string { return fmt.Sprintf("field %s: %s", e.FieldID, e.Err) }

func (e *DestArgError) ClientError() bool { return true }

func (r *Registry) ValidateDest(ctx context.Context, dest gadb.DestV1) error {
p := r.Provider(dest.Type)
if p == nil {
Expand Down
2 changes: 2 additions & 0 deletions notification/nfydest/validateaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type ActionParamError struct {

func (e *ActionParamError) Error() string { return fmt.Sprintf("parameter %s: %s", e.ParamID, e.Err) }

func (e *ActionParamError) ClientError() bool { return true }

func (r *Registry) ValidateAction(ctx context.Context, act gadb.UIKActionV1) error {
p := r.Provider(act.Dest.Type)
if p == nil {
Expand Down

0 comments on commit d99a530

Please sign in to comment.