Fix User Error in Async API Endpoint (#9911)

This commit is contained in:
Joe Haddad 2020-01-02 18:47:39 -05:00 committed by GitHub
parent d59858e32c
commit 00179adcaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 1 deletions

View file

@ -54,7 +54,7 @@ export async function apiResolver(
apiRes.json = data => sendJson(apiRes, data)
const resolver = interopDefault(resolverModule)
resolver(req, res)
await resolver(req, res)
} catch (err) {
if (err instanceof ApiError) {
sendError(apiRes, err.statusCode, err.message)

View file

@ -0,0 +1,3 @@
export default async (req, res) => {
throw new Error('User error')
}

View file

@ -63,6 +63,13 @@ function runTests(dev = false) {
expect(text).toBe('Internal Server Error')
})
it('should throw Internal Server Error (async)', async () => {
const res = await fetchViaHTTP(appPort, '/api/user-error-async', null, {})
const text = await res.text()
expect(res.status).toBe(500)
expect(text).toBe('Internal Server Error')
})
it('should parse JSON body', async () => {
const data = await fetchViaHTTP(appPort, '/api/parse', null, {
method: 'POST',