Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer to wrap third party errors #243

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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