mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 02:01:40 -05:00
add try/catch and test for rejected options
This commit is contained in:
parent
782cb20864
commit
68bba72ff8
2 changed files with 22 additions and 1 deletions
|
@ -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)) {
|
||||
try {
|
||||
optionsObject = await options(req);
|
||||
} catch (e) {
|
||||
res.status(500);
|
||||
res.send(`Invalid options provided to ApolloServer: ${e.message}`);
|
||||
}
|
||||
} else {
|
||||
optionsObject = options;
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue