Skip to content

Commit

Permalink
Prefer to wrap third party errors (#243)
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Coffman <[email protected]>

Signed-off-by: Steve Coffman <[email protected]>
  • Loading branch information
StevenACoffman authored Sep 23, 2022
1 parent b3be96f commit bbe8d04
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
7 changes: 7 additions & 0 deletions gqlerror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func WrapPath(path ast.Path, err error) *Error {
}
}

func Wrap(err error) *Error {
return &Error{
err: err,
Message: err.Error(),
}
}

func Errorf(message string, args ...interface{}) *Error {
return &Error{
Message: fmt.Sprintf(message, args...),
Expand Down
19 changes: 15 additions & 4 deletions gqlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import (
)

func LoadSchema(str ...*ast.Source) (*ast.Schema, error) {
return validator.LoadSchema(append([]*ast.Source{validator.Prelude}, str...)...)
ast, err := validator.LoadSchema(append([]*ast.Source{validator.Prelude}, str...)...)
gqlErr, ok := err.(*gqlerror.Error)
if ok {
return ast, gqlErr
}
if err != nil {
return ast, gqlerror.Wrap(err)
}
return ast, nil
}

func MustLoadSchema(str ...*ast.Source) *ast.Schema {
Expand All @@ -25,11 +33,14 @@ func MustLoadSchema(str ...*ast.Source) *ast.Schema {
func LoadQuery(schema *ast.Schema, str string) (*ast.QueryDocument, gqlerror.List) {
query, err := parser.ParseQuery(&ast.Source{Input: str})
if err != nil {
gqlErr := err.(*gqlerror.Error)
return nil, gqlerror.List{gqlErr}
gqlErr, ok := err.(*gqlerror.Error)
if ok {
return nil, gqlerror.List{gqlErr}
}
return nil, gqlerror.List{gqlerror.Wrap(err)}
}
errs := validator.Validate(schema, query)
if errs != nil {
if len(errs) > 0 {
return nil, errs
}

Expand Down
8 changes: 6 additions & 2 deletions parser/testrunner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ func Test(t *testing.T, filename string, f func(t *testing.T, input string) Spec

if spec.Error == nil {
if result.Error != nil {
gqlErr := err.(*gqlerror.Error)
t.Errorf("unexpected error %s", gqlErr.Message)
gqlErr, ok := err.(*gqlerror.Error)
if ok {
t.Errorf("unexpected error %s", gqlErr.Message)
} else {
t.Errorf("unexpected error %+v", gqlerror.Wrap(err))
}
}
} else if result.Error == nil {
t.Errorf("expected error but got none")
Expand Down
10 changes: 9 additions & 1 deletion validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ func AddRule(name string, f ruleFunc) {

func Validate(schema *Schema, doc *QueryDocument) gqlerror.List {
var errs gqlerror.List

if schema == nil {
errs = append(errs, gqlerror.Errorf("cannot validate as Schema is nil"))
}
if doc == nil {
errs = append(errs, gqlerror.Errorf("cannot validate as QueryDocument is nil"))
}
if len(errs) > 0 {
return errs
}
observers := &Events{}
for i := range rules {
rule := rules[i]
Expand Down

0 comments on commit bbe8d04

Please sign in to comment.