-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
updated custom error func #94
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ type QueryError struct { | |
Path []interface{} `json:"path,omitempty"` | ||
Rule string `json:"-"` | ||
ResolverError error `json:"-"` | ||
ExtraInfo interface{} `json:"extraInfo,omitempty"` | ||
} | ||
|
||
type Location struct { | ||
|
@@ -29,10 +30,11 @@ func Errorf(format string, a ...interface{}) *QueryError { | |
|
||
// WithMessagef is the same as Errorf, except it will store the err inside | ||
// the ResolverError field. | ||
func WithMessagef(err error, format string, a ...interface{}) *QueryError { | ||
func WithMessagef(err error, extra interface{}, format string, a ...interface{}) *QueryError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the worst kind of BC break.
|
||
return &QueryError{ | ||
Message: fmt.Sprintf(format, a...), | ||
ResolverError: err, | ||
ExtraInfo: extra, | ||
} | ||
} | ||
|
||
|
@@ -49,25 +51,35 @@ func (err *QueryError) Error() string { | |
|
||
var _ error = &QueryError{} | ||
|
||
// FormattedError a formatted error which includes the error message and extra information | ||
// which is JSON encoded and passed with the error. | ||
type FormattedError struct { | ||
Message string | ||
Extra interface{} | ||
} | ||
|
||
// ErrorMessageFunc a func which given an error returns a formatted error. | ||
type ErrorMessageFunc func(err error) FormattedError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is quite right. It only lets the user change whats in this "extra" key. Maybe it could return a graphql.Marshaler? |
||
|
||
type Builder struct { | ||
Errors []*QueryError | ||
// ErrorMessageFn will be used to generate the error | ||
// message from errors given to Error(). | ||
// | ||
// If ErrorMessageFn is nil, err.Error() will be used. | ||
ErrorMessageFn func(error) string | ||
ErrorMessageFn ErrorMessageFunc | ||
} | ||
|
||
func (c *Builder) Errorf(format string, args ...interface{}) { | ||
c.Errors = append(c.Errors, Errorf(format, args...)) | ||
} | ||
|
||
func (c *Builder) Error(err error) { | ||
var gqlErrMessage string | ||
fErr := FormattedError{Message: err.Error()} | ||
|
||
if c.ErrorMessageFn != nil { | ||
gqlErrMessage = c.ErrorMessageFn(err) | ||
} else { | ||
gqlErrMessage = err.Error() | ||
fErr = c.ErrorMessageFn(err) | ||
} | ||
c.Errors = append(c.Errors, WithMessagef(err, gqlErrMessage)) | ||
|
||
c.Errors = append(c.Errors, WithMessagef(err, fErr.Extra, fErr.Message)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ import ( | |
"github.com/vektah/gqlgen/test/models" | ||
) | ||
|
||
type fErr = gqlerrors.FormattedError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of abusing type aliases like this. |
||
|
||
func TestCompiles(t *testing.T) {} | ||
|
||
func TestErrorConverter(t *testing.T) { | ||
|
@@ -28,11 +30,11 @@ func TestErrorConverter(t *testing.T) { | |
require.Nil(t, errs) | ||
|
||
t.Run("with", func(t *testing.T) { | ||
testConvErr := func(e error) string { | ||
testConvErr := func(e error) fErr { | ||
if _, ok := errors.Cause(e).(*specialErr); ok { | ||
return "override special error message" | ||
return fErr{Message: "override special error message"} | ||
} | ||
return e.Error() | ||
return fErr{Message: e.Error()} | ||
} | ||
t.Run("special error", func(t *testing.T) { | ||
resolvers.nestedOutputsErr = &specialErr{} | ||
|
@@ -68,7 +70,7 @@ func TestErrorConverter(t *testing.T) { | |
}) | ||
} | ||
|
||
func mkctx(doc *query.Document, errFn func(e error) string) context.Context { | ||
func mkctx(doc *query.Document, errFn gqlerrors.ErrorMessageFunc) context.Context { | ||
return graphql.WithRequestContext(context.Background(), &graphql.RequestContext{ | ||
Doc: doc, | ||
ResolverMiddleware: func(ctx context.Context, next graphql.Resolver) (res interface{}, err error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an error being ignored here, I wonder why gometalinter isn't complaining.