add try/catch and test for rejected options

This commit is contained in:
Jonas Helfer 2016-07-05 14:19:14 -07:00
parent 782cb20864
commit 68bba72ff8
2 changed files with 22 additions and 1 deletions

View file

@ -31,7 +31,12 @@ export function graphqlHTTP(options: ApolloOptions | ExpressApolloOptionsFunctio
return async (req: express.Request, res: express.Response, next) => {
let optionsObject: ApolloOptions;
if (isOptionsFunction(options)) {
optionsObject = await options(req);
try {
optionsObject = await options(req);
} catch (e) {
res.status(500);
res.send(`Invalid options provided to ApolloServer: ${e.message}`);
}
} else {
optionsObject = options;
}

View file

@ -102,6 +102,22 @@ export default (createApp: CreateAppFunc) => {
});
});
it('throws an error if options promise is rejected', () => {
const app = createApp({ apolloOptions: () => {
return Promise.reject({}) as any as ApolloOptions
}});
const expected = 'Invalid options';
const req = request(app)
.post('/graphql')
.send({
query: 'query test{ testString }',
});
return req.then((res) => {
expect(res.status).to.equal(500);
return expect(res.error.text).to.contain(expected);
});
});
it('throws an error if POST body is missing', () => {
const app = createApp({excludeParser: true});
const req = request(app)