Skip to content

Commit 281a85f

Browse files
authored
fix: Handle Response Errors Correctly (#236)
* Handle Response Errors Correctly It looks like during a refactor this logic got changed incorrectly. You need to await the promise in this catch block. Otherwise any error thrown inside `responseViaResponseObject` won't get caught and will bubble out the top. * feat: add test * chore: lint/format
1 parent 99ad170 commit 281a85f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/listener.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export const getRequestListener = (
224224
}
225225

226226
try {
227-
return responseViaResponseObject(res, outgoing, options)
227+
return await responseViaResponseObject(res, outgoing, options)
228228
} catch (e) {
229229
return handleResponseError(e, outgoing)
230230
}

test/listener.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ describe('Invalid request', () => {
6464
expect(res.status).toBe(400)
6565
})
6666
})
67+
68+
describe('malformed body response', () => {
69+
const malformedResponse = {
70+
body: 'content',
71+
}
72+
const requestListener = getRequestListener(() => malformedResponse, {
73+
hostname: 'example.com',
74+
})
75+
const server = createServer(requestListener)
76+
77+
it('Should return a 500 for a malformed response', async () => {
78+
const res = await request(server).get('/').send()
79+
expect(res.status).toBe(500)
80+
})
81+
})
6782
})
6883

6984
describe('Error handling - sync fetchCallback', () => {

0 commit comments

Comments
 (0)