From bbe8d040dc30abda3085554b4aea2e89a313f322 Mon Sep 17 00:00:00 2001 From: Steve Coffman Date: Fri, 23 Sep 2022 11:19:43 -0400 Subject: [PATCH] Prefer to wrap third party errors (#243) Signed-off-by: Steve Coffman Signed-off-by: Steve Coffman --- gqlerror/error.go | 7 +++++++ gqlparser.go | 19 +++++++++++++++---- parser/testrunner/runner.go | 8 ++++++-- validator/validator.go | 10 +++++++++- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/gqlerror/error.go b/gqlerror/error.go index 8145061a..2620d593 100644 --- a/gqlerror/error.go +++ b/gqlerror/error.go @@ -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...), diff --git a/gqlparser.go b/gqlparser.go index 69a2cf18..3a6e2d13 100644 --- a/gqlparser.go +++ b/gqlparser.go @@ -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 { @@ -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 } diff --git a/parser/testrunner/runner.go b/parser/testrunner/runner.go index 5eea2389..b1f35504 100644 --- a/parser/testrunner/runner.go +++ b/parser/testrunner/runner.go @@ -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") diff --git a/validator/validator.go b/validator/validator.go index c5426ada..b4f37ce2 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -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]