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

Avoid sending user errors to client #8005

Merged
merged 3 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/next-server/server/api-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export async function apiResolver(
if (e instanceof ApiError) {
sendError(res, e.statusCode, e.message)
} else {
sendError(res, 500, e.message)
console.error(e)
sendError(res, 500, 'Internal Server Error')
}
}
}
Expand Down Expand Up @@ -226,7 +227,7 @@ export function sendError(
) {
res.statusCode = statusCode
res.statusMessage = message
res.end()
res.end(message)
}

interface LazyProps {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export default class Server {
}
}

apiResolver(
await apiResolver(
req,
res,
params,
Expand Down
3 changes: 3 additions & 0 deletions test/integration/api-support/pages/api/user-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (req, res) => {
throw new Error('User error')
}
7 changes: 7 additions & 0 deletions test/integration/api-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ function runTests (serverless = false) {
expect(json).toEqual({ error: 'Server error!' })
})

it('should throw Internal Server Error', async () => {
const res = await fetchViaHTTP(appPort, '/api/user-error', 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',
Expand Down