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

fix: handle the error thrown by fetchCallback #10

Merged
merged 1 commit into from
Nov 29, 2022
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
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