graphql-zeus icon indicating copy to clipboard operation
graphql-zeus copied to clipboard

Getting operationName and query string when an error occurs

Open CryptoGraffe opened this issue 2 years ago • 5 comments

Is there any way to get operationName and query string when there is an error? The GraphQLError object is pretty sparse.

CryptoGraffe avatar May 20 '22 21:05 CryptoGraffe

Good one. We should output better errors

aexol avatar May 22 '22 07:05 aexol

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.

GauBen avatar May 24 '22 14:05 GauBen

Here are some of my thoughts on what this class should include:

  1. The operationName and the query and variables and any other obvious context.
  2. more fields on the errors array, as code and path are usually there.
  3. The console.error() log is troublesome for things like automated error capture.
  4. The string should ideally elevate the underlying error message, rather than just GraphQL Response Error

CryptoGraffe avatar May 24 '22 15:05 CryptoGraffe

  • 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

GauBen avatar Jul 13 '22 07:07 GauBen

Yes, please do not log from inside the error. The calling code should handle the logging.

akkie avatar Mar 21 '23 17:03 akkie