graphql-zeus
graphql-zeus copied to clipboard
Getting operationName and query string when an error occurs
Is there any way to get operationName and query string when there is an error? The GraphQLError object is pretty sparse.
Good one. We should output better errors
Hey there, I'm interested in having some error details too.
The current error class is:
export class GraphQLError extends Error {
constructor(public response: GraphQLResponse) {
super("");
console.error(response);
}
toString() {
return "GraphQL Response Error";
}
}
I would love it if it looked like this:
export class GraphQLError extends Error {
data: unknown;
errors: Array<{message: string, [k: string]: any}>; // TODO
constructor(public response: GraphQLResponse) {
super("GraphQLError");
this.data = response.data;
this.errors = response.errors;
}
toString() {
return "GraphQL Response Error";
}
}
I'm not sure about the design though.
Here are some of my thoughts on what this class should include:
- The
operationName
and thequery
andvariables
and any other obvious context. - more fields on the errors array, as
code
andpath
are usually there. - The console.error() log is troublesome for things like automated error capture.
- The string should ideally elevate the underlying error message, rather than just
GraphQL Response Error
- 2 and 3 will be fixed by #321
- 4 is not that easy considering that there can be several errors, I tried to create a meaningful error message, but you might find room for improvement there
Yes, please do not log from inside the error. The calling code should handle the logging.