Skip to content

Commit

Permalink
feat: handle returned errors (closes #28)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 27, 2021
1 parent b10b496 commit 991fcff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export function createHandle (stack: Stack): PHandle {
} else if (type === 'object' && val !== undefined) {
if (val.buffer) {
return send(res, val)
} else if (val instanceof Error) {
throw createError(val)
} else {
return send(res, JSON.stringify(val, null, 2), MIMES.json)
}
Expand Down
14 changes: 14 additions & 0 deletions test/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,18 @@ describe('error', () => {
const res = await request.get('/')
expect(res.status).toBe(500)
})

it('can handle returned Error', async () => {
app.use('/', () => new Error('failed'), { promisify: true })

const res = await request.get('/')
expect(res.status).toBe(500)
})

it('can handle returned H3Error', async () => {
app.use('/', () => createError({ statusCode: 501 }), { promisify: true })

const res = await request.get('/')
expect(res.status).toBe(501)
})
})

0 comments on commit 991fcff

Please sign in to comment.