Skip to content

Commit

Permalink
Update rewriteError in docs
Browse files Browse the repository at this point in the history
- Deprecate `formatError` in docs in favor of `rewriteError`
- lint-fix
  • Loading branch information
michaelwatson93 committed Apr 16, 2019
1 parent b18302c commit 21b8b86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
40 changes: 21 additions & 19 deletions docs/source/features/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ When an error occurs in Apollo server both inside and outside of resolvers, each
The first step to improving the usability of a server is providing the error stack trace by default. The following example demonstrates the response returned from Apollo server with a resolver that throws a node [`SystemError`](https://nodejs.org/api/errors.html#errors_system_errors).

```js line=14-16
const {
ApolloServer,
gql,
const {
ApolloServer,
gql,
} = require('apollo-server');

const typeDefs = gql`
Expand Down Expand Up @@ -45,10 +45,10 @@ 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
const {
ApolloServer,
gql,
AuthenticationError,
const {
ApolloServer,
gql,
AuthenticationError,
} = require('apollo-server');

const typeDefs = gql`
Expand All @@ -72,13 +72,13 @@ The response will return:

## Augmenting error details

When clients provide bad input, you may want to return additional information
like a localized message for each field or argument that was invalid. The
When clients provide bad input, you may want to return additional information
like a localized message for each field or argument that was invalid. The
following example demonstrates how you can use `UserInputError` to augment
your error messages with additional details.

```js line=15-21
const {
const {
ApolloServer,
UserInputError,
gql,
Expand Down Expand Up @@ -114,23 +114,25 @@ application, you can use the base `ApolloError` class.

```js
new ApolloError(message, code, additionalProperties);
```
```

## Masking and logging errors

The Apollo server constructor accepts a `formatError` function that is run on each error passed back to the client. This can be used to mask errors as well as for logging.
This example demonstrates masking (or suppressing the stacktrace):
The Apollo server constructor accepts a `rewriteError` function that is run on each error passed back to the client or trace reporting. This can be used to mask errors as well as for logging. This example demonstrates masking (or suppressing the error):

```js line=4-10
```js line=4-12
const server = new ApolloServer({
typeDefs,
resolvers,
formatError: error => {
rewriteError: error => {
console.log(error);
return new Error('Internal server error');
// Or, you can delete the exception information
// delete error.extensions.exception;
// return error;
return new GraphQLError('rewritten as a new error');

//Or, you can delete the exception information
return null

//Or return the original error
return error;
},
});

Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-engine-reporting/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class EngineReportingAgent<TContext = any> {
if (
this.sendReportsImmediately ||
this.reportSize >=
(this.options.maxUncompressedReportSize || 4 * 1024 * 1024)
(this.options.maxUncompressedReportSize || 4 * 1024 * 1024)
) {
this.sendReportAndReportErrors();
}
Expand Down Expand Up @@ -273,7 +273,7 @@ export class EngineReportingAgent<TContext = any> {
// redirects.
throw new Error(
`Error sending report to Apollo Engine servers (HTTP status ${
response.status
response.status
}): ${await response.text()}`,
);
}
Expand Down

0 comments on commit 21b8b86

Please sign in to comment.