Skip to content

Commit

Permalink
Merge pull request #174 from nicolaslopezj/catch-formatError
Browse files Browse the repository at this point in the history
Catch formatError
  • Loading branch information
Sashko Stubailo committed Oct 17, 2016
2 parents e4ae188 + c1c75d0 commit 36ae6d4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
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

0 comments on commit 36ae6d4

Please sign in to comment.