apollo-server/packages/apollo-server-core/src/formatters.ts
Evans Hauser e0f7052fb1
Errors Lifecycle: user extensions > engine reporting > formatError (#1272)
* enable willSendResponse to return a modified response

* add formatError as an extension that wraps engine reporting

* ensure that formatError once on every error path

* move old formatError express tests into integration suite

* add error lifecycle with minimal engine reporting check

* increase granularity of formatError test

* return 400 error for GraphQL error created by context

* add check for internal server error for errors thrown in context

* comment about context error status code
2018-06-29 10:36:52 -07:00

29 lines
815 B
TypeScript

import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions';
import { formatApolloErrors } from 'apollo-server-errors';
export class FormatErrorExtension extends GraphQLExtension {
private formatError: Function;
private debug: boolean;
public constructor(formatError: Function, debug: boolean = false) {
super();
this.formatError = formatError;
this.debug = debug;
}
public willSendResponse(o: {
graphqlResponse: GraphQLResponse;
}): void | { graphqlResponse: GraphQLResponse } {
if (o.graphqlResponse.errors) {
return {
graphqlResponse: {
...o.graphqlResponse,
errors: formatApolloErrors(o.graphqlResponse.errors, {
formatter: this.formatError,
debug: this.debug,
}),
},
};
}
}
}