Skip to content

Commit 36d0bd3

Browse files
authoredJun 24, 2024··
Add request method and URL to error messages (#595)
1 parent 585ebcb commit 36d0bd3

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed
 

‎source/errors/HTTPError.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class HTTPError extends Error {
1212
const status = `${code} ${title}`.trim();
1313
const reason = status ? `status code ${status}` : 'an unknown error';
1414

15-
super(`Request failed with ${reason}`);
15+
super(`Request failed with ${reason}: ${request.method} ${request.url}`);
1616

1717
this.name = 'HTTPError';
1818
this.response = response;

‎source/errors/TimeoutError.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export class TimeoutError extends Error {
22
public request: Request;
33

44
constructor(request: Request) {
5-
super('Request timed out');
5+
super(`Request timed out: ${request.method} ${request.url}`);
66
this.name = 'TimeoutError';
77
this.request = request;
88
}

‎test/browser.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ defaultBrowsersTest(
244244
throw new TypeError('Expected to have an object error');
245245
}
246246

247-
t.is(error.message, 'TimeoutError: Request timed out');
247+
t.is(error.message, `TimeoutError: Request timed out: GET ${server.url}/slow`);
248248
t.is(error.request.url, `${server.url}/slow`);
249249
},
250250
);
@@ -480,7 +480,7 @@ browserTest('retry with body', [chromium, webkit], async (t: ExecutionContext, p
480480
method: 'PUT',
481481
retry: 1,
482482
}), server.url),
483-
{message: /HTTPError: Request failed with status code 502 Bad Gateway/},
483+
{message: /HTTPError: Request failed with status code 502 Bad Gateway: PUT/},
484484
);
485485

486486
t.is(requestCount, 2);

‎test/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ test('beforeRetry hook with parseJson and error.response.json()', async t => {
482482
beforeRetry: [
483483
async ({error, retryCount}) => {
484484
t.true(error instanceof HTTPError);
485-
t.is(error.message, 'Request failed with status code 502 Bad Gateway');
485+
t.is(error.message, `Request failed with status code 502 Bad Gateway: GET ${server.url}/`);
486486
t.true((error as HTTPError).response instanceof Response);
487487
t.deepEqual(await (error as HTTPError).response.json(), {awesome: true});
488488
t.is(retryCount, 1);

‎test/http-error.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ test('HTTPError handles undefined response.statusText', t => {
2020
// not define statusText, such as IE, Safari, etc.
2121
// See https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText#Browser_compatibility
2222
createFakeResponse({statusText: undefined, status}),
23+
new Request('invalid:foo'),
2324
);
2425

25-
t.is(error.message, 'Request failed with status code 500');
26+
t.is(error.message, 'Request failed with status code 500: GET invalid:foo');
2627
});
2728

2829
test('HTTPError handles undefined response.status', t => {
@@ -31,17 +32,19 @@ test('HTTPError handles undefined response.status', t => {
3132
// This simulates a catastrophic case where some unexpected
3233
// response object was sent to HTTPError.
3334
createFakeResponse({statusText: undefined, status: undefined}),
35+
new Request('invalid:foo'),
3436
);
3537

36-
t.is(error.message, 'Request failed with an unknown error');
38+
t.is(error.message, 'Request failed with an unknown error: GET invalid:foo');
3739
});
3840

3941
test('HTTPError handles a response.status of 0', t => {
4042
// @ts-expect-error missing Request
4143
const error = new HTTPError(
4244
// Apparently, it's possible to get a response status of 0.
4345
createFakeResponse({statusText: undefined, status: 0}),
46+
new Request('invalid:foo'),
4447
);
4548

46-
t.is(error.message, 'Request failed with status code 0');
49+
t.is(error.message, 'Request failed with status code 0: GET invalid:foo');
4750
});

0 commit comments

Comments
 (0)
Please sign in to comment.