Skip to content

Commit 6dd7574

Browse files
committed
Improve HTTPError message to include method and URL
Fixes #1126
1 parent 1c71194 commit 6dd7574

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

source/core/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class HTTPError<T = any> extends RequestError<T> {
9696
declare readonly timings: Timings;
9797

9898
constructor(response: PlainResponse) {
99-
super(`Response code ${response.statusCode} (${response.statusMessage!})`, {}, response.request);
99+
super(`Request failed with status code ${response.statusCode} (${response.statusMessage!}): ${response.request.options.method} ${response.request.options.url!.toString()}`, {}, response.request);
100100
this.name = 'HTTPError';
101101
this.code = 'ERR_NON_2XX_3XX_RESPONSE';
102102
}

test/error.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test('properties', withServer, async (t, server, got) => {
2828
t.true(Object.prototype.propertyIsEnumerable.call(error, 'options'));
2929
t.false(Object.prototype.propertyIsEnumerable.call(error, 'response'));
3030
t.is(error.code, 'ERR_NON_2XX_3XX_RESPONSE');
31-
t.is(error.message, 'Response code 404 (Not Found)');
31+
t.regex(error.message, /^Request failed with status code 404 \(Not Found\): GET http:\/\/localhost:\d+\/$/);
3232
t.deepEqual(error.options.url, url);
3333
t.is(error.response.headers.connection, 'keep-alive');
3434
// Assert is used for type checking
@@ -78,7 +78,7 @@ test('default status message', withServer, async (t, server, got) => {
7878
const error = await t.throwsAsync<HTTPError>(got(''),
7979
{
8080
instanceOf: HTTPError,
81-
message: 'Response code 400 (Bad Request)',
81+
message: /^Request failed with status code 400 \(Bad Request\): GET http:\/\/localhost:\d+\/$/,
8282
});
8383
t.is(error?.response.statusCode, 400);
8484
t.is(error?.response.statusMessage, 'Bad Request');
@@ -94,7 +94,7 @@ test('custom status message', withServer, async (t, server, got) => {
9494
const error = await t.throwsAsync<HTTPError>(got(''),
9595
{
9696
instanceOf: HTTPError,
97-
message: 'Response code 400 (Something Exploded)',
97+
message: /^Request failed with status code 400 \(Something Exploded\): GET http:\/\/localhost:\d+\/$/,
9898
});
9999
t.is(error?.response.statusCode, 400);
100100
t.is(error?.response.statusMessage, 'Something Exploded');
@@ -109,7 +109,7 @@ test('custom body', withServer, async (t, server, got) => {
109109
const error = await t.throwsAsync<HTTPError>(got(''),
110110
{
111111
instanceOf: HTTPError,
112-
message: 'Response code 404 (Not Found)',
112+
message: /^Request failed with status code 404 \(Not Found\): GET http:\/\/localhost:\d+\/$/,
113113
});
114114
t.is(error?.response.statusCode, 404);
115115
// Typecheck for default `any` type
@@ -128,7 +128,7 @@ test('custom json body', withServer, async (t, server, got) => {
128128
const error = await t.throwsAsync<HTTPError<{message: string}>>(got('', {responseType: 'json'}),
129129
{
130130
instanceOf: HTTPError,
131-
message: 'Response code 404 (Not Found)',
131+
message: /^Request failed with status code 404 \(Not Found\): GET http:\/\/localhost:\d+\/$/,
132132
});
133133
t.is(error?.response.statusCode, 404);
134134
// Assert is used for body typecheck
@@ -150,7 +150,7 @@ test('contains Got options', withServer, async (t, server, got) => {
150150
const error = await t.throwsAsync<HTTPError>(got(options),
151151
{
152152
instanceOf: HTTPError,
153-
message: 'Response code 404 (Not Found)',
153+
message: /^Request failed with status code 404 \(Not Found\): GET http:\/\/localhost:\d+\/$/,
154154
});
155155
t.is(error?.response.statusCode, 404);
156156
t.is(error?.options.context.foo, options.context.foo);
@@ -165,7 +165,7 @@ test('empty status message is overriden by the default one', withServer, async (
165165
const error = await t.throwsAsync<HTTPError>(got(''),
166166
{
167167
instanceOf: HTTPError,
168-
message: 'Response code 400 (Bad Request)',
168+
message: /^Request failed with status code 400 \(Bad Request\): GET http:\/\/localhost:\d+\/$/,
169169
});
170170
t.is(error?.response.statusCode, 400);
171171
t.is(error?.response.statusMessage, http.STATUS_CODES[400]);

test/hooks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ test('no infinity loop when retrying on afterResponse', withServer, async (t, se
853853
}),
854854
],
855855
},
856-
}), {instanceOf: HTTPError, message: 'Response code 401 (Unauthorized)'});
856+
}), {instanceOf: HTTPError, message: /^Request failed with status code 401 \(Unauthorized\): GET http:\/\/localhost:\d+\/$/});
857857
});
858858

859859
test('throws on afterResponse retry failure', withServer, async (t, server, got) => {
@@ -888,7 +888,7 @@ test('throws on afterResponse retry failure', withServer, async (t, server, got)
888888
},
889889
],
890890
},
891-
}), {instanceOf: HTTPError, message: 'Response code 500 (Internal Server Error)'});
891+
}), {instanceOf: HTTPError, message: /^Request failed with status code 500 \(Internal Server Error\): GET http:\/\/localhost:\d+\/$/});
892892
});
893893

894894
test('does not throw on afterResponse retry HTTP failure if throwHttpErrors is false', withServer, async (t, server, got) => {
@@ -1174,7 +1174,7 @@ test('beforeError is called with an error - stream', withServer, async (t, _serv
11741174
return error2;
11751175
}],
11761176
},
1177-
})), {message: 'Response code 404 (Not Found)'});
1177+
})), {message: /^Request failed with status code 404 \(Not Found\): GET http:\/\/localhost:\d+\/$/});
11781178
});
11791179

11801180
test('beforeError allows modifications', async t => {
@@ -1314,7 +1314,7 @@ test('hooks are not duplicated', withServer, async (t, _server, got) => {
13141314
retry: {
13151315
limit: 0,
13161316
},
1317-
}), {message: 'Response code 404 (Not Found)'});
1317+
}), {message: /^Request failed with status code 404 \(Not Found\): GET http:\/\/localhost:\d+\/$/});
13181318

13191319
t.is(calls, 1);
13201320
});

test/response-parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ test('ignores errors on invalid non-200 responses', withServer, async (t, server
120120

121121
const error = await t.throwsAsync<HTTPError>(got({responseType: 'json', retry: {limit: 0}}), {
122122
instanceOf: HTTPError,
123-
message: 'Response code 500 (Internal Server Error)',
123+
message: /^Request failed with status code 500 \(Internal Server Error\): GET http:\/\/localhost:\d+\/$/,
124124
});
125125

126126
t.is(error?.response.body, 'Internal error');

test/retry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ test('does not break on redirect', withServer, async (t, server, got) => {
404404
response.end();
405405
});
406406

407-
await t.throwsAsync(got('redirect'), {message: 'Response code 500 (Internal Server Error)'});
407+
await t.throwsAsync(got('redirect'), {message: /^Request failed with status code 500 \(Internal Server Error\): GET http:\/\/localhost:\d+\/$/});
408408
t.is(tries, 1);
409409
});
410410

test/stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ test('has error event', withServer, async (t, server, got) => {
139139
const stream = got.stream('');
140140
await t.throwsAsync(pEvent(stream, 'response'), {
141141
instanceOf: HTTPError,
142-
message: 'Response code 404 (Not Found)',
142+
message: /^Request failed with status code 404 \(Not Found\): GET http:\/\/localhost:\d+\/$/,
143143
});
144144
});
145145

0 commit comments

Comments
 (0)