Skip to content

Commit

Permalink
fix: handle the error thrown by fetchCallback (honojs#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Nov 29, 2022
1 parent ac6f89f commit 3d50cba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ const getRequestListener = (fetchCallback: FetchCallback) => {
init['body'] = buffer
}

const res = (await fetchCallback(new Request(url.toString(), init))) as Response
let res: Response

try {
res = (await fetchCallback(new Request(url.toString(), init))) as Response
} catch {
res = new Response(null, { status: 500 })
}

const contentType = res.headers.get('content-type') || ''

Expand Down
5 changes: 5 additions & 0 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ describe('Error handling', () => {
expect(res.status).toBe(500)
expect(res.text).toBe('Custom Error!')
})

it('Should return 500 response - PURGE method', async () => {
const res = await request(server).purge('/')
expect(res.status).toBe(500)
})
})

describe('Basic Auth Middleware', () => {
Expand Down

0 comments on commit 3d50cba

Please sign in to comment.