Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Errors do not bubble up to formatError #6

Open
timrs2998 opened this issue Jan 8, 2018 · 5 comments
Open

Errors do not bubble up to formatError #6

timrs2998 opened this issue Jan 8, 2018 · 5 comments

Comments

@timrs2998
Copy link

timrs2998 commented Jan 8, 2018

Summary

I'm creating an issue here so you're aware, but I ultimately believe this is an issue with graphql-tools. When throwing a GrampsError, graphql-tools removes all properties except error.message and rethrows a new error. As a result, the formatError provided by the @gramps/errors package is useless.

Walkthrough

When a GrampsError is thrown, it is caught by graphql-tools stitching logic and rethrown:

https://github.com/apollographql/graphql-tools/blob/master/src/stitching/errors.ts#L84

    const errorMessage = result.errors
      .map((error: { message: string }) => error.message)
      .join('\n');
    throw locatedError(
      new Error(errorMessage),
      ..,
      ..,
    );

Later, the new error bubbles up to the formatError logic from @gramps/errors:

import { formatError } from '@gramps/errors'

app.use(graphqlExpress({ formatError })

However, the formatError function does not receive the expected GrampsErrors, rather, it receives a different error object transformed and rethrown by graphql-tools. It cannot log the targetEndpoint, statusCode, docsLink, and other fields on a GrampsError because the error has been caught and rethrown by graphql-tools.

I can write a short gist to reproduce it if you want.

@jlengstorf
Copy link
Member

I noticed this and haven't had the time to dig into it, unfortunately. 😢

I'm putting together a roadmap for Q1 that includes figuring out why this stopped working and either patching it or looking for the root cause in graphql-tools, if it's there. If someone else has time before I get to it, that would be very much appreciated.

@timrs2998
Copy link
Author

timrs2998 commented Jan 10, 2018

As a workaround, we've decided on a hack solution of serializing JSON into the error message string, and deserializing later in formatError

@timrs2998
Copy link
Author

@ryanomackey
Copy link
Member

Yeah, this seems like a valid workaround:

In your data source model:

  handleError(response) {
      const serialized = JSON.stringify(GrampsError({
        statusCode: response.statusCode,
        description: response.error.message,
        errorCode: response.error.code,
        targetEndpoint: response.options.uri,
        graphqlModel: this.constructor.name
      }));

      throw Error(serialized);
  }

And when you need to call formatError:

gramps({
  ...
  apollo: {
    graphqlExpress: {
      formatError: formatError(logger)(JSON.parse(err.message)),
    },
  },
});

Less than ideal, for sure.

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

No branches or pull requests

3 participants