Skip to content

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

@valeeum

Description

@valeeum

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;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions