apollo-server icon indicating copy to clipboard operation
apollo-server copied to clipboard

EngineReportingOptions.rewriteError hides error subtypes

Open lpgera opened this issue 5 years ago • 11 comments

First of all, huge thanks for taking my original PR further and implementing rewriteError in https://github.com/apollographql/apollo-server/pull/2618! 🙇

I was trying to get rewriteError to work with our Apollo Engine integration, but checking the type of the error like in the example doesn't work for me, because AuthenticationError and ForbiddenError are converted to GraphQLError somewhere along the way, and the only way to distinguish them from other errors is checking the error code.

I created a repo which can be used to reproduce the issue: https://github.com/lpgera/apollo-server-rewriteerror-testing

After setting the apiKey, and running a hello query the isAuthenticationError should be true in the log, but it's always false.

Am I missing something or is the documentation wrong about checking the type of the error with instanceof?

lpgera avatar Jun 19 '19 12:06 lpgera

Thanks for reporting this and for providing the reproduction code.

Is it possible that you need to check err.originalError instanceof AuthenticationError? Could you try?

abernix avatar Aug 28 '19 19:08 abernix

No luck with that either, err.originalError is undefined. 😞

lpgera avatar Aug 31 '19 11:08 lpgera

We were having the same issue until I just figured out that graphql-tools was swallowing the errors at some point during the stitching through mergeSchema().

robross0606 avatar Oct 02 '19 16:10 robross0606

Looks like the TreeBuilder is cloning the original error which then ditches all custom properties:

https://github.com/apollographql/apollo-server/blob/master/packages/apollo-engine-reporting/src/treeBuilder.ts#L189

rdougan avatar Feb 14 '20 18:02 rdougan

Has anybody solved this issue? I believe we're running into this now, at least in some cases.

jacob-israel-turner avatar Jan 22 '21 23:01 jacob-israel-turner

Ok, I was able to get around this by drilling into error.extensions.code in rewriteError. For AuthenticationError, the code is UNAUTHENTICATED, and for UserInputError, the code is BAD_USER_INPUT. While this workaround should work, this is clearly different than what's in the documentation. Either the rewriteError method should be updated to retain the type of the original error, or the documentation should be updated to show how to get the code, and the codes should be readily available as well.

jacob-israel-turner avatar Jan 23 '21 00:01 jacob-israel-turner

it's been over two years since this was reported and it still hasn't been addressed.... any updates @abernix? If this isn't going to be fixed the documentation should be updated.

jkepps avatar Nov 23 '21 21:11 jkepps

I think the simplest thing to do here would be to change the docs to compare codes rather than use instanceof, since that seems less brittle. Happy to take a PR to fix that. (I'm not sure I 100% understand the issue as the code in traceTreeBuilder.ts does appear to try to preserve the prototype and properties?)

glasser avatar Dec 01 '21 00:12 glasser

I think the simplest thing to do here would be to change the docs to compare codes rather than use instanceof, since that seems less brittle.

@glasser This is what we have in our project and it still isn't working. We are still getting an extremely high rate of errors in apollo studio for mutations that handle login and form submissions that are simply the result of invalid inputs.

ApolloServerPluginUsageReporting({
 rewriteError(error) {
    if (
      error.extensions?.code === 'BAD_USER_INPUT' ||
      error.extensions?.code === 'UNAUTHENTICATED' ||
      error.extensions?.code === 'FORBIDDEN'
    ) {
      return null
    }
    return error
  },
})

@trevor-scheer @abernix anything?

jkepps avatar Jan 13 '22 01:01 jkepps

I don't think I'll be able to help you debug this without a complete reproduction (eg something I can git clone or look at in codesandbox.io).

glasser avatar Jan 13 '22 06:01 glasser

yeah that's alright @glasser, appreciate the response.

i think i was able to solve it by including both the UsageReporting plugin and the InlineTracing plugin and passing the rewriteError function to both. for some reason it appears the inline tracing filters the errors in my prod env but not my dev, and the usage reporting filters in my dev env, but not prod

jkepps avatar Jan 13 '22 17:01 jkepps

I think the simplest thing to do here would be to change the docs to compare codes rather than use instanceof, since that seems less brittle. Happy to take a PR to fix that. (I'm not sure I 100% understand the issue as the code in traceTreeBuilder.ts does appear to try to preserve the prototype and properties?)

We leaned into this approach: in Apollo Server 4 there are no special error subclasses (in fact, even ApolloError is gone), and we explicitly document looking at error.extensions.code to figure out what error you have (and export an enum covering the codes for the errors that Apollo Server's own code produces).

glasser avatar Oct 20 '22 00:10 glasser