Skip to content

Commit

Permalink
Add request method and URL to error messages (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
sholladay committed Jun 24, 2024
1 parent 585ebcb commit 36d0bd3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion source/errors/HTTPError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class HTTPError extends Error {
const status = `${code} ${title}`.trim();
const reason = status ? `status code ${status}` : 'an unknown error';

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

this.name = 'HTTPError';
this.response = response;
Expand Down
2 changes: 1 addition & 1 deletion source/errors/TimeoutError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export class TimeoutError extends Error {
public request: Request;

constructor(request: Request) {
super('Request timed out');
super(`Request timed out: ${request.method} ${request.url}`);
this.name = 'TimeoutError';
this.request = request;
}
Expand Down
4 changes: 2 additions & 2 deletions test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ defaultBrowsersTest(
throw new TypeError('Expected to have an object error');
}

t.is(error.message, 'TimeoutError: Request timed out');
t.is(error.message, `TimeoutError: Request timed out: GET ${server.url}/slow`);
t.is(error.request.url, `${server.url}/slow`);
},
);
Expand Down Expand Up @@ -480,7 +480,7 @@ browserTest('retry with body', [chromium, webkit], async (t: ExecutionContext, p
method: 'PUT',
retry: 1,
}), server.url),
{message: /HTTPError: Request failed with status code 502 Bad Gateway/},
{message: /HTTPError: Request failed with status code 502 Bad Gateway: PUT/},
);

t.is(requestCount, 2);
Expand Down
2 changes: 1 addition & 1 deletion test/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ test('beforeRetry hook with parseJson and error.response.json()', async t => {
beforeRetry: [
async ({error, retryCount}) => {
t.true(error instanceof HTTPError);
t.is(error.message, 'Request failed with status code 502 Bad Gateway');
t.is(error.message, `Request failed with status code 502 Bad Gateway: GET ${server.url}/`);
t.true((error as HTTPError).response instanceof Response);
t.deepEqual(await (error as HTTPError).response.json(), {awesome: true});
t.is(retryCount, 1);
Expand Down
9 changes: 6 additions & 3 deletions test/http-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ test('HTTPError handles undefined response.statusText', t => {
// not define statusText, such as IE, Safari, etc.
// See https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText#Browser_compatibility
createFakeResponse({statusText: undefined, status}),
new Request('invalid:foo'),
);

t.is(error.message, 'Request failed with status code 500');
t.is(error.message, 'Request failed with status code 500: GET invalid:foo');
});

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

t.is(error.message, 'Request failed with an unknown error');
t.is(error.message, 'Request failed with an unknown error: GET invalid:foo');
});

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

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

0 comments on commit 36d0bd3

Please sign in to comment.