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

Add BadUserInputError and TransientError as extensions of ApolloError #1143

Merged
merged 12 commits into from
Jun 8, 2018

Conversation

clarencenpy
Copy link
Contributor

@clarencenpy clarencenpy commented Jun 5, 2018

Proposed additions of two new error types. They are just simple wrappers around the ApolloError interface, and could be used to highlight more use cases of using ApolloErrors.

TODO:

  • Update CHANGELOG.md with your change (include reference to issue & this PR)
  • Make sure all of the significant new logic is covered by tests
  • Rebase your changes on master so that they can be merged easily
  • Make sure all tests and linter rules pass

@clarencenpy clarencenpy requested a review from evans June 5, 2018 08:41
@ghost ghost added the ⛲️ feature New addition or enhancement to existing solutions label Jun 5, 2018
Copy link
Contributor

@evans evans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BadUserInputError is a great addition! Adding it and the other errors to the api reference would be awesome too. Pointing to the error reference section in the error handling section would make them more visible as well!

I think for now TransientError would be better as a suggested pattern, rather than a baked in error. Currently the name seems a bit too general and its functionality isn't super well specified. As we build the capabilities into apollo server, such as rate limiting, it could be confusing.

}
}

export class TransientError extends ApolloError {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave the TransientError out for now, since we don't have a compelling use case on the client-side. It seems like the best place to handle theses sorts of errors is either in the layer above the GraphQL execution, such as middleware, or in the Data sources, which could retry on their own. I totally agree that we want this sort of retriability. I'm just not convinced that an error in the resolvers is the right place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. Let's keep it as a suggested pattern so that our use cases for ApolloError stays solid!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working updating the error handling section of the docs as well. What do you feel about moving some of the code examples from my blog over as well once they have been reviewed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally! We can move them over now too if you prefer

errorClass: BadUserInputError,
name: 'BadUserInputError',
});
console.log(`error: ${JSON.stringify(error, null, 2)}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove logs from the code. Ideally it would be caught in the linting step. I guess prettier doesn't do that for us anymore 😢

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes thanks for catching that🙏🙏

@evans
Copy link
Contributor

evans commented Jun 5, 2018

We should also make sure that the base branch is version-2, rather than the old refactor-2.0

@evans evans changed the base branch from refactor-2.0 to version-2 June 5, 2018 23:14
@ghost ghost added the ⛲️ feature New addition or enhancement to existing solutions label Jun 5, 2018
@evans evans added this to the Release 2.0 milestone Jun 6, 2018
@clarencenpy
Copy link
Contributor Author

@evans the tests are now failing because I updated it so that it checks for properties that was passed in ApolloError() in extensions.exception instead of the root level of the error.

Copy link
Contributor

@evans evans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clarencenpy Great stuff! After we get the tests passing, plus new screenshot and a couple cosmetic changes to the code samples, we'll be all set.

@@ -101,12 +96,20 @@ The response will return:

![Screenshot demonstrating augmented error](../images/features/error-user-input.png)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update this screenshot to match the new code

@@ -43,11 +42,7 @@ The response will return:
In addition to stacktraces, Apollo Server's exported errors specify a human-readable string in the `code` field of `extensions` that enables the client to perform corrective actions. In addition to improving the client experience, the `code` field allows the server to categorize errors. For example, an `AuthenticationError` sets the code to `UNAUTHENTICATED`, which enables the client to reauthenticate and would generally be ignored as a server anomaly.

```js line=4,15-17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These highlighted lines should line up with the AuthenticationError and it's usage, so I think we want to keep them on separate lines even though it might not match Prettier's styling

errorClass: BadUserInputError,
name: 'BadUserInputError',
});
expect(error.extensions.exception.field1).to.equal('property1');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make these fields on the root error. The important part is for when these errors are thrown in resolvers to contain the correct layout. Here for example

@ghost ghost added the ⛲️ feature New addition or enhancement to existing solutions label Jun 8, 2018
@clarencenpy
Copy link
Contributor Author

@evans should be good to go!

@evans evans merged commit 5f30792 into version-2 Jun 8, 2018
@evans
Copy link
Contributor

evans commented Jun 8, 2018

@clarencenpy Awesome work! We'll release this in the next beta 🎉

@abernix
Copy link
Member

abernix commented Jun 12, 2018

Purely talking points:

  1. Is there a particular reason for the Bad prefix on BadUserInputError? It would seem that UserInputError might still accurately represent a user-input error.
  2. Since these are all being exported from apollo-server, and are presumably all extended versions of ApolloError, would it be at all beneficial to prefix them with ApolloError (e.g. ApolloErrorUserInput, etc.). (Thinking about intellisense/auto-completion/discoverability)

@clarencenpy
Copy link
Contributor Author

@abernix My apologies, should have seen your comments earlier!

  1. (copied from slack) I see your point there! I guess one reason why that naming makes sense is that it maps directly to the error code in the response, which is BAD_USER_INPUT. The convention is similar for the other error types.
  2. I'm all for this if we dont think the names get too verbose? Intellisense does offer completions when we type error in the import { ... } from 'apollo-server' statement though, that might be good enough for discoverability?

@clarencenpy clarencenpy deleted the clarencenpy/proposed-error-types branch June 19, 2018 18:19
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
⛲️ feature New addition or enhancement to existing solutions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants