Skip to content
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

Logging Graphql Errors Along with Metadata from 'req' Object #106

Open
valeeum opened this issue Apr 1, 2022 · 2 comments
Open

Logging Graphql Errors Along with Metadata from 'req' Object #106

valeeum opened this issue Apr 1, 2022 · 2 comments

Comments

@valeeum
Copy link

valeeum commented Apr 1, 2022

I'm looking for some direction on how to log graphql related errors while also adding relevant information from the req object. I'm able to log the error object using the serverOptions.formatError function but unable to access the req object from here.

      serverOptions: {
        formatError(err) {
          logger.error({ err }); // looking to add userID here which is available from jwt token in request object
          return err;
        },
      },

I don't believe the following snippet from service.js ever actually triggers the this.sendError function:

   try {
      await this.prepareGraphQLSchema();
      return this.graphqlHandler(req, res);
   } catch (err) {
      this.sendError(req, res, err);
   }

...because moleculerApollo.js swallows the error by returning undefined in the catch handler:

	try {
		const { graphqlResponse, responseInit } = await runHttpQuery([req, res], {
			method: req.method,
			options,
			query,
			request: convertNodeHttpToRequest(req),
		});

		setHeaders(res, responseInit.headers);

		return graphqlResponse;
	} catch (error) {
		if ("HttpQueryError" === error.name && error.headers) {
			setHeaders(res, error.headers);
		}

		if (!error.statusCode) {
			error.statusCode = 500;
		}

		res.statusCode = error.statusCode || error.code || 500;
		res.end(error.message);

		return undefined;
	}
@shawnmcknight
Copy link
Member

I think the issue here is that this.graphqlHandler is an async function. By returning without awaiting the catch block for this.sendError gets bypassed. I think this is fixed with return await this.graphqlHandler which should allow the catch block to remain in scope and be able to hit the this.sendError.

Can you try out that change and see if it does the trick for you?

@valeeum
Copy link
Author

valeeum commented Apr 8, 2022

I think that's part of the solution. The other issue is that the this.graphqlHandlerfunction in moleculerApollo.js has a try/catch block that doesn't rethrow the error and instead returns undefined when an error is caught. this.sendError is never triggered in service.js.

	try {
		const { graphqlResponse, responseInit } = await runHttpQuery([req, res], {
			method: req.method,
			options,
			query,
			request: convertNodeHttpToRequest(req),
		});

		setHeaders(res, responseInit.headers);

		return graphqlResponse;
	} catch (error) {
		if ("HttpQueryError" === error.name && error.headers) {
			setHeaders(res, error.headers);
		}

		if (!error.statusCode) {
			error.statusCode = 500;
		}

		res.statusCode = error.statusCode || error.code || 500;
		res.end(error.message);

		return undefined;
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants