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

Update error masking example in the docs #2865

Closed
wants to merge 1 commit into from

Conversation

borekb
Copy link
Contributor

@borekb borekb commented Jun 16, 2019

formatError expects the returned error to be of type GraphQLFormattedError and the example was using return new Error(...) which caused TypeScript error.

The updated code just rewrites the message without changing the rest of the object.

`formatError` expects the returned error to be of type GraphQLFormattedError and the example was using `return new Error(...)` which made TypeScript unhappy.
@abernix
Copy link
Member

abernix commented Jun 19, 2019

Out of curiosity, have you verified that making the change in this way (as in, modifying the existing error) doesn't change the representation of the error which is used in non-response avenues? Specifically, the error received by apollo-engine-reporting's rewriteError [#2618] method, and the error received by the didEncounterError life-cycle hook [#2719])?

@borekb
Copy link
Contributor Author

borekb commented Jun 19, 2019

I didn't – I have actually never used rewriteError or didEncounterError.

Sorry if this PR isn't valid but just copy/pasting the example to our code threw errors.

@abernix
Copy link
Member

abernix commented Jun 30, 2019

Related to #1884, perhaps? @borekb, I'm curious if that solution does the trick for you?

import { ApolloError } from 'apollo-server-errors';
throw new ApolloError('Error!');

(Just want to point out that it's unfortunate that this is the way that formatError works and that a more clean solution hasn't been implemented earlier, but we'll get there!)

@borekb
Copy link
Contributor Author

borekb commented Jul 1, 2019

@abernix I'm not sure I understand – do you mean I should throw an error instead of returning it?


To sum up (all inside formatError), this what the docs say:

if (err.message.startsWith("Database Error: ")) {
  return new Error('Internal server error');
}

This is what works for me:

if (err.message.startsWith("Database Error: ")) {
  err.message = 'Internal server error';
}

Is this what you're proposing?

if (err.message.startsWith("Database Error: ")) {
  throw new ApolloError('Internal server error');
}

@abernix
Copy link
Member

abernix commented Jul 9, 2019

Superseded by #2966. Hope that works for you! We'll get it into a release soon.

@abernix abernix closed this Jul 9, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants