Add request method and URL to error messages (#595)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Seth Holladay · committed by GitHub · parent 585ebcb · Browse files · 5 files·+11 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+11 added−8 removed
- tests:+9 added−6 removed
- other:+2 added−2 removed
Provenance
- Commit
36d0bd3- Tree
65ebb37- Author
Seth Holladay <me@seth-holladay.com>- Committer
GitHub <noreply@github.com>- Parent
585ebcb
5 files changed+11 added−8 removed
modified:source/errors/HTTPError.ts+1 added-1 removed
10 unmodified lines111213141515161718193 unmodified lines10 unmodified lines const title = response.statusText || ''; 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; this.request = request;3 unmodified linesmodified:source/errors/TimeoutError.ts+1 added-1 removed
1234556789export 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; }}modified:test/browser.ts+2 added-2 removed
242 unmodified lines243 if (typeof error !== 'object') {244 throw new TypeError('Expected to have an object error');245 }246 247- t.is(error.message, 'TimeoutError: Request timed out');247+ t.is(error.message, `TimeoutError: Request timed out: GET ${server.url}/slow`);248 t.is(error.request.url, `${server.url}/slow`);249 },250 );251 227 unmodified lines479 body: 'foo',480 method: 'PUT',481 retry: 1,482 }), 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/},484 );485 486 t.is(requestCount, 2);487 });modified:test/hooks.ts+1 added-1 removed
480 unmodified lines481 hooks: {482 beforeRetry: [483 async ({error, retryCount}) => {484 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}/`);486 t.true((error as HTTPError).response instanceof Response);487 t.deepEqual(await (error as HTTPError).response.json(), {awesome: true});488 t.is(retryCount, 1);489 t.is(requestCount, 1);185 unmodified linesmodified:test/http-error.ts+6 added-3 removed
18 unmodified lines192021222324252526272829303132333435363736383940414243444546474846495018 unmodified lines // This simulates the case where a browser Response object does // 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 => { // @ts-expect-error missing Request const error = new HTTPError( // 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');});