Skip to content

Commit

Permalink
Avoid sending user errors to client (vercel#8005)
Browse files Browse the repository at this point in the history
* Avoid sending user error to client Closes vercel#7526

* Small improvments

* Update sending errors
  • Loading branch information
huv1k authored and ijjk committed Jul 23, 2019
1 parent ff415c4 commit a28299d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
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

0 comments on commit a28299d

Please sign in to comment.