-
Notifications
You must be signed in to change notification settings - Fork 840
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
Implement June 2018 error spec #363
Conversation
LGTM. @chris-ramon When to merge this feature? |
@@ -6,9 +6,16 @@ import ( | |||
"github.com/graphql-go/graphql/language/location" | |||
) | |||
|
|||
type ExtendedError interface { | |||
error | |||
ErrorExtensions() map[string]interface{} |
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.
I think we should be naming this function Extensions
to avoid extra suffix.
It is addressed in a new PR: #364
When do you plan to release this? We would love to have that change available 👍 |
Hi @SebScoFr, thanks a lot for using the lib! I've just created a new release |
@ccbrown I am working on this "extension", may I have a simple example that implement this feature, as I find some difficult to implement the ExtendedError interface ? Thanks for the great PR |
@jphuc96 here's an example: type ErrorWithCode struct {
Message string
Code string
}
func (err *ErrorWithCode) Error() string {
return err.Message
}
func (err *ErrorWithCode) Extensions() map[string]interface{} {
return map[string]interface{}{
"code": err.Code,
}
}
// Not required but recommended:
// Makes sure at compile time that ErrorWithCode implements gqlerrors.ExtendedError
var _ gqlerrors.ExtendedError = (*ErrorWithCode)(nil) Then in your resolver you can do something like this: return nil, &ErrorWithCode{"Not authorized.", "not_authorized"} |
that really did the trick, thanks for saving me @ccbrown 👍 👍 👍 |
@jphuc96 You can ref the example code: https://github.com/go-ggz/ggz/blob/d3d22a347506cbe36ef24bdd229efb74a56c0541/pkg/errors/errors.go#L1 // Error applicational
type Error struct {
Type Type
Message string
cause error
}
// Error message
func (e *Error) Error() string {
return e.Message
}
// Cause of the original error
func (e *Error) Cause() string {
if e.cause != nil {
return e.cause.Error()
}
return ""
}
// Extensions for graphQL extension
func (e *Error) Extensions() map[string]interface{} {
if e.cause != nil {
log.Error().Err(e.cause).Msg("graphql error report")
}
return map[string]interface{}{
"code": e.Type.Code(),
"type": e.Type,
}
} |
This implements section 7.1.2 of the June 2018 spec.
Paths are now added to errors:
And extensions are now present when resolve functions return implementations of
gqlerrors.ExtendedError
:It's very much a transliteration of the JavaScript implementation. No creativity was involved here. I've gone through all the tests and painstakingly updated them. Those in addition to some new tests cover the changes extremely well.
This addresses #200, #259, #351, #109, and kinda sorta #312.