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

Catch formatError #174

Merged
merged 8 commits into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Pass `ctx` instead of `ctx.request` to options function in Koa integration ([@HriBB](https://github.com/HriBB)) in [PR #154](https://github.com/apollostack/apollo-server/pull/154)
* Manage TypeScript declaration files using npm. ([@od1k](https:/github.com/od1k) in [#162](https://github.com/apollostack/apollo-server/pull/162))
* Fix connect example in readme. ([@conrad-vanl](https://github.com/conrad-vanl) in [#165](https://github.com/apollostack/apollo-server/pull/165))
* Add try/catch to formatError. ([@nicolaslopezj](https://github.com/nicolaslopezj) in [#174](https://github.com/apollostack/apollo-server/pull/174))

### v0.3.2
* Added missing exports for hapi integration ([@nnance](https://github.com/nnance)) in [PR #152](https://github.com/apollostack/apollo-server/pull/152)
Expand Down
18 changes: 13 additions & 5 deletions src/core/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,19 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResult> {
logFunction({action: LogAction.request, step: LogStep.start});

function format(errors: Array<Error>): Array<Error> {
// TODO: fix types! shouldn't have to cast.
// the blocker is that the typings aren't right atm:
// GraphQLResult returns Array<GraphQLError>, but the formatError function
// returns Array<GraphQLFormattedError>
return errors.map(options.formatError || formatError as any) as Array<Error>;
return errors.map((error) => {
if (options.formatError) {
try {
return options.formatError(error);
} catch (err) {
console.error('Error in formatError function:', err);
const newError = new Error('Internal server error');
return formatError(newError);
}
} else {
return formatError(error);
}
}) as Array<Error>;
}

function printStackTrace(error: Error) {
Expand Down
17 changes: 17 additions & 0 deletions src/integrations/integrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,23 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
});
});

it('sends internal server error when formatError fails', () => {
app = createApp({apolloOptions: {
schema: Schema,
formatError: (err) => {
throw new Error('I should be catched');
},
}});
const req = request(app)
.post('/graphql')
.send({
query: 'query test{ testError }',
});
return req.then((res) => {
return expect(res.res.body.errors[0].message).to.equal('Internal server error');
});
});

it('sends stack trace to error if debug mode is set', () => {
const expected = /at resolveOrError/;
const stackTrace = [];
Expand Down