From 00179adcaa3140f74786d4cd07c0adc7ad6ae01d Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Thu, 2 Jan 2020 18:47:39 -0500 Subject: [PATCH] Fix User Error in Async API Endpoint (#9911) --- packages/next/next-server/server/api-utils.ts | 2 +- test/integration/api-support/pages/api/user-error-async.js | 3 +++ test/integration/api-support/test/index.test.js | 7 +++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/integration/api-support/pages/api/user-error-async.js diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index e9f7b1911b..8e200b2ec8 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -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) diff --git a/test/integration/api-support/pages/api/user-error-async.js b/test/integration/api-support/pages/api/user-error-async.js new file mode 100644 index 0000000000..06214200c2 --- /dev/null +++ b/test/integration/api-support/pages/api/user-error-async.js @@ -0,0 +1,3 @@ +export default async (req, res) => { + throw new Error('User error') +} diff --git a/test/integration/api-support/test/index.test.js b/test/integration/api-support/test/index.test.js index 115c965dd7..b422a4a35a 100644 --- a/test/integration/api-support/test/index.test.js +++ b/test/integration/api-support/test/index.test.js @@ -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',