Skip to content

Commit d09d8cb

Browse files
committed
fix: correctly handle errors from onUnhandledRequest vs handler exceptions
1 parent 055ad91 commit d09d8cb

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/core/new/on-unhandled-frame.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function onUnhandledFrame(
3636
devUtils.error('Error: %s', message)
3737

3838
return Promise.reject(
39-
new Error(
39+
new InternalError(
4040
devUtils.formatMessage(
4141
'Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
4242
),

src/core/new/sources/interceptor-source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ class InterceptorHttpNetworkFrame extends HttpNetworkFrame {
144144
}
145145

146146
if (reason instanceof InternalError) {
147-
throw reason
147+
this.#controller.errorWith(reason as any)
148148
}
149149

150-
this.#controller.errorWith(reason as any)
150+
throw reason
151151
}
152152

153153
public passthrough(): void {

test/node/rest-api/response/throw-response.node.test.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@ afterAll(() => {
1818

1919
it('supports throwing a plain Response in a response resolver', async () => {
2020
server.use(
21-
http.get('https://example.com/', () => {
21+
http.get('http://localhost/resource', () => {
2222
// You can throw a Response instance in a response resolver
2323
// to short-circuit its execution and respond "early".
2424
throw new Response('hello world')
2525
}),
2626
)
2727

28-
const response = await fetch('https://example.com')
28+
const response = await fetch('http://localhost/resource')
2929

3030
expect(response.status).toBe(200)
3131
await expect(response.text()).resolves.toBe('hello world')
3232
})
3333

3434
it('supports throwing an HttpResponse instance in a response resolver', async () => {
3535
server.use(
36-
http.get('https://example.com/', () => {
36+
http.get('http://localhost/resource', () => {
3737
throw HttpResponse.text('hello world')
3838
}),
3939
)
4040

41-
const response = await fetch('https://example.com')
41+
const response = await fetch('http://localhost/resource')
4242

4343
expect(response.status).toBe(200)
4444
expect(response.headers.get('Content-Type')).toBe('text/plain')
@@ -47,12 +47,12 @@ it('supports throwing an HttpResponse instance in a response resolver', async ()
4747

4848
it('supports throwing an error response in a response resolver', async () => {
4949
server.use(
50-
http.get('https://example.com/', () => {
50+
http.get('http://localhost/resource', () => {
5151
throw HttpResponse.text('not found', { status: 400 })
5252
}),
5353
)
5454

55-
const response = await fetch('https://example.com')
55+
const response = await fetch('http://localhost/resource')
5656

5757
expect(response.status).toBe(400)
5858
expect(response.headers.get('Content-Type')).toBe('text/plain')
@@ -61,17 +61,19 @@ it('supports throwing an error response in a response resolver', async () => {
6161

6262
it('supports throwing a network error in a response resolver', async () => {
6363
server.use(
64-
http.get('https://example.com/', () => {
64+
http.get('http://localhost/resource', () => {
6565
throw HttpResponse.error()
6666
}),
6767
)
6868

69-
await expect(fetch('https://example.com')).rejects.toThrow('Failed to fetch')
69+
await expect(fetch('http://localhost/resource')).rejects.toThrow(
70+
'Failed to fetch',
71+
)
7072
})
7173

7274
it('supports middleware-style responses', async () => {
7375
server.use(
74-
http.get('https://example.com/', ({ request }) => {
76+
http.get('http://localhost/resource', ({ request }) => {
7577
const url = new URL(request.url)
7678

7779
if (!url.searchParams.has('id')) {
@@ -82,25 +84,25 @@ it('supports middleware-style responses', async () => {
8284
}),
8385
)
8486

85-
const response = await fetch('https://example.com/?id=1')
87+
const response = await fetch('http://localhost/resource?id=1')
8688
expect(response.status).toBe(200)
8789
expect(response.headers.get('Content-Type')).toBe('text/plain')
8890
await expect(response.text()).resolves.toBe('ok')
8991

90-
const errorResponse = await fetch('https://example.com/')
92+
const errorResponse = await fetch('http://localhost/resource')
9193
expect(errorResponse.status).toBe(400)
9294
expect(errorResponse.headers.get('Content-Type')).toBe('text/plain')
9395
await expect(errorResponse.text()).resolves.toBe('must have id')
9496
})
9597

9698
it('coerces non-response errors into 500 error responses', async () => {
9799
server.use(
98-
http.get('https://example.com/', () => {
100+
http.get('http://localhost/resource', () => {
99101
throw new Error('Custom error')
100102
}),
101103
)
102104

103-
const response = await fetch('https://example.com')
105+
const response = await fetch('http://localhost/resource')
104106

105107
expect(response.status).toBe(500)
106108
expect(response.statusText).toBe('Unhandled Exception')

0 commit comments

Comments
 (0)