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 5 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
17 changes: 12 additions & 5 deletions src/core/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ 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);
return formatError(error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

People might be using formatError to hide some information, for example maybe they don't want the error message to leak to the client. I'd suggest the best option is to return a generic "Internal server error" message instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
} else {
return formatError(error);
}
}) as Array<Error>;
}

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

it('should use default formatter if provided formatError fails', () => {
app = createApp({apolloOptions: {
schema: Schema,
formatError: (err) => {
throw new Error('I should be catched');
},
}});
const logStub = stub(console, 'error');
const expected = /at resolveOrError/;
app = createApp({apolloOptions: {
schema: Schema,
debug: true,
}});
const req = request(app)
.post('/graphql')
.send({
query: 'query test{ testError }',
});
return req.then((res) => {
logStub.restore();
expect(logStub.callCount).to.equal(1);
return expect(logStub.getCall(0).args[0]).to.match(expected);
});
});

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