-
Notifications
You must be signed in to change notification settings - Fork 842
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
Add a path
field to FormattedError
?
#259
Comments
In case if somebody decides to implement that, don't just add a path, but also it'd be nice to be able to do custom formatting on errors. Error should be an interface. For example see this article: https://medium.com/@tarkus/validation-and-user-errors-in-graphql-mutations-39ca79cd00bf GraphQL express middleware in js land has this hook "formatError", which allows you to return whatever you want as an error. The nice property of GraphQL errors themselves is that they can infer path info for you, but other than that it's very limited in a sense that there is only a message. Some people want custom error codes, etc. So it should be something like: type CustomError interface {
SetLocation(...)
SetPath(...)
ToJSON() []byte
error
} When a resolver returns an error, graphql lib should be able to set path/location for you, however what that error contains and how it produces JSON version of itself at the end is up to a lib user. |
This is done in #363. And in regards to @nsf's comment, errors are now customizable to the extent that the spec allows: If your resolvers return an error that implements
|
Can anybody post an example? I try to add extensions to func (r resolver) LoginUser(params graphql.ResolveParams) (interface{}, error) {
...
if err != nil {
errf := gqlerrors.FormatError(err)
errf.Extensions = map[string]interface{}{"code": "INVALID_ARGUMENT"}
return nil, errf
}
return response, nil
} GraphQl Response: {
"data": {
"LoginUser": null
},
"errors": [
{
"message": "Invalid argument",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"LoginUser"
]
}
]
} |
@Raze92 Don’t return |
That worked. Thanks! |
Like this:
https://github.com/graphql/graphql-js/blob/fed3ef58fde674fa96d79879eb4dfc24a0ba1db0/src/error/formatError.js#L30
I think this is very useful.
The text was updated successfully, but these errors were encountered: