mirror of
https://github.com/vale981/apollo-server
synced 2025-03-16 15:56:41 -04:00

* 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
29 lines
815 B
TypeScript
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,
|
|
}),
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|