Skip to content

Commit 0e167b8

Browse files
HTTPError code set to 'HTTPError' #1711 (#1739)
1 parent f896aa5 commit 0e167b8

12 files changed

+93
-38
lines changed

readme.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -1705,43 +1705,44 @@ Additionaly, the errors may have `request` (Got Stream) and `response` (Got Resp
17051705
17061706
#### got.RequestError
17071707
1708-
When a request fails. Contains a `code` property with error class code, like `ECONNREFUSED`. All the errors below inherit this one.
1708+
When a request fails. Contains a `code` property with error class code, like `ECONNREFUSED`. If there is no specific code supplied, `code` defaults to `ERR_GOT_REQUEST_ERROR`. All the errors below inherit this one.
17091709
17101710
#### got.CacheError
17111711
1712-
When a cache method fails, for example, if the database goes down or there's a filesystem error.
1712+
When a cache method fails, for example, if the database goes down or there's a filesystem error. Contains a `code` property with `ERR_CACHE_ACCESS` or a more specific failure code.
17131713
17141714
#### got.ReadError
17151715
1716-
When reading from response stream fails.
1716+
When reading from response stream fails. Contains a `code` property with `ERR_READING_RESPONSE_STREAM` or a more specific failure code.
17171717
17181718
#### got.ParseError
17191719
1720-
When server response code is 2xx, and parsing body fails. Includes a `response` property.
1720+
When server response code is 2xx, and parsing body fails. Includes a `response` property. Contains a `code` property with `ERR_BODY_PARSE_FAILURE` or a more specific failure code.
17211721
17221722
#### got.UploadError
17231723
1724-
When the request body is a stream and an error occurs while reading from that stream.
1724+
When the request body is a stream and an error occurs while reading from that stream. Contains a `code` property with `ERR_UPLOAD` or a more specific failure code.
17251725
17261726
#### got.HTTPError
17271727
1728-
When the server response code is not 2xx nor 3xx if `options.followRedirect` is `true`, but always except for 304. Includes a `response` property.
1728+
When the server response code is not 2xx nor 3xx if `options.followRedirect` is `true`, but always except for 304. Includes a `response` property. Contains a `code` property with `ERR_NON_2XX_3XX_RESPONSE` or a more specific failure code.
1729+
17291730
17301731
#### got.MaxRedirectsError
17311732
1732-
When the server redirects you more than ten times. Includes a `response` property.
1733+
When the server redirects you more than ten times. Includes a `response` property. Contains a `code` property with `ERR_TOO_MANY_REDIRECTS`.
17331734
17341735
#### got.UnsupportedProtocolError
17351736
1736-
When given an unsupported protocol.
1737+
When given an unsupported protocol. Contains a `code` property with `ERR_UNSUPPORTED_PROTOCOL`.
17371738
17381739
#### got.TimeoutError
17391740
1740-
When the request is aborted due to a [timeout](#timeout). Includes an `event` and `timings` property.
1741+
When the request is aborted due to a [timeout](#timeout). Includes an `event` and `timings` property. Contains a `code` property with `ETIMEDOUT`.
17411742
17421743
#### got.CancelError
17431744
1744-
When the request is aborted with `.cancel()`.
1745+
When the request is aborted with `.cancel()`. Contains a `code` property with `ERR_CANCELED`.
17451746
17461747
## Aborting the request
17471748

source/as-promise/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export class ParseError extends RequestError {
267267

268268
super(`${error.message} in "${options.url.toString()}"`, error, response.request);
269269
this.name = 'ParseError';
270+
this.code = this.code === 'ERR_GOT_REQUEST_ERROR' ? 'ERR_BODY_PARSE_FAILURE' : this.code;
270271
}
271272
}
272273

@@ -279,6 +280,7 @@ export class CancelError extends RequestError {
279280
constructor(request: Request) {
280281
super('Promise was canceled', {}, request);
281282
this.name = 'CancelError';
283+
this.code = 'ERR_CANCELED';
282284
}
283285

284286
get isCanceled() {

source/core/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ An error to be thrown when a request fails.
11791179
Contains a `code` property with error class code, like `ECONNREFUSED`.
11801180
*/
11811181
export class RequestError extends Error {
1182-
code?: string;
1182+
code: string;
11831183
stack!: string;
11841184
declare readonly options: NormalizedOptions;
11851185
readonly response?: Response;
@@ -1191,7 +1191,7 @@ export class RequestError extends Error {
11911191
Error.captureStackTrace(this, this.constructor);
11921192

11931193
this.name = 'RequestError';
1194-
this.code = error.code;
1194+
this.code = error.code ?? 'ERR_GOT_REQUEST_ERROR';
11951195

11961196
if (self instanceof Request) {
11971197
Object.defineProperty(this, 'request', {
@@ -1249,6 +1249,7 @@ export class MaxRedirectsError extends RequestError {
12491249
constructor(request: Request) {
12501250
super(`Redirected ${request.options.maxRedirects} times. Aborting.`, {}, request);
12511251
this.name = 'MaxRedirectsError';
1252+
this.code = 'ERR_TOO_MANY_REDIRECTS';
12521253
}
12531254
}
12541255

@@ -1264,6 +1265,7 @@ export class HTTPError extends RequestError {
12641265
constructor(response: Response) {
12651266
super(`Response code ${response.statusCode} (${response.statusMessage!})`, {}, response.request);
12661267
this.name = 'HTTPError';
1268+
this.code = 'ERR_NON_2XX_3XX_RESPONSE';
12671269
}
12681270
}
12691271
/**
@@ -1276,6 +1278,7 @@ export class CacheError extends RequestError {
12761278
constructor(error: Error, request: Request) {
12771279
super(error.message, error, request);
12781280
this.name = 'CacheError';
1281+
this.code = this.code === 'ERR_GOT_REQUEST_ERROR' ? 'ERR_CACHE_ACCESS' : this.code;
12791282
}
12801283
}
12811284

@@ -1288,6 +1291,7 @@ export class UploadError extends RequestError {
12881291
constructor(error: Error, request: Request) {
12891292
super(error.message, error, request);
12901293
this.name = 'UploadError';
1294+
this.code = this.code === 'ERR_GOT_REQUEST_ERROR' ? 'ERR_UPLOAD' : this.code;
12911295
}
12921296
}
12931297

@@ -1319,6 +1323,7 @@ export class ReadError extends RequestError {
13191323
constructor(error: Error, request: Request) {
13201324
super(error.message, error, request);
13211325
this.name = 'ReadError';
1326+
this.code = this.code === 'ERR_GOT_REQUEST_ERROR' ? 'ERR_READING_RESPONSE_STREAM' : this.code;
13221327
}
13231328
}
13241329

@@ -1329,6 +1334,7 @@ export class UnsupportedProtocolError extends RequestError {
13291334
constructor(options: NormalizedOptions) {
13301335
super(`Unsupported protocol "${options.url.protocol}"`, {}, options);
13311336
this.name = 'UnsupportedProtocolError';
1337+
this.code = 'ERR_UNSUPPORTED_PROTOCOL';
13321338
}
13331339
}
13341340

source/types.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -262,58 +262,60 @@ export interface Got extends Record<HTTPAlias, GotRequestFunction>, GotRequestFu
262262
defaults: InstanceDefaults;
263263

264264
/**
265-
An error to be thrown when a cache method fails.
266-
For example, if the database goes down or there's a filesystem error.
265+
An error to be thrown when a cache method fails. For example, if the database goes down or there's a filesystem error.
266+
Contains a `code` property with `ERR_CACHE_ACCESS` or a more specific failure code.
267267
*/
268268
CacheError: typeof CacheError;
269269

270270
/**
271-
An error to be thrown when a request fails.
272-
Contains a `code` property with error class code, like `ECONNREFUSED`.
271+
An error to be thrown when a request fails. Contains a `code` property with error class code, like `ECONNREFUSED`.
272+
If there is no specific code supplied, `code` defaults to `ERR_GOT_REQUEST_ERROR`.
273273
*/
274274
RequestError: typeof RequestError;
275275

276276
/**
277-
An error to be thrown when reading from response stream fails.
277+
An error to be thrown when reading from response stream fails. Contains a `code` property with
278+
`ERR_READING_RESPONSE_STREAM` or a more specific failure code.
278279
*/
279280
ReadError: typeof ReadError;
280281

281282
/**
282-
An error to be thrown when server response code is 2xx, and parsing body fails.
283-
Includes a `response` property.
283+
An error to be thrown when server response code is 2xx, and parsing body fails. Includes a
284+
`response` property. Contains a `code` property with `ERR_BODY_PARSE_FAILURE` or a more specific failure code.
284285
*/
285286
ParseError: typeof ParseError;
286287

287288
/**
288289
An error to be thrown when the server response code is not 2xx nor 3xx if `options.followRedirect` is `true`, but always except for 304.
289-
Includes a `response` property.
290+
Includes a `response` property. Contains a `code` property with `ERR_NON_2XX_3XX_RESPONSE` or a more specific failure code.
290291
*/
291292
HTTPError: typeof HTTPError;
292293

293294
/**
294295
An error to be thrown when the server redirects you more than ten times.
295-
Includes a `response` property.
296+
Includes a `response` property. Contains a `code` property with `ERR_TOO_MANY_REDIRECTS`.
296297
*/
297298
MaxRedirectsError: typeof MaxRedirectsError;
298299

299300
/**
300-
An error to be thrown when given an unsupported protocol.
301+
An error to be thrown when given an unsupported protocol. Contains a `code` property with `ERR_UNSUPPORTED_PROTOCOL`.
301302
*/
302303
UnsupportedProtocolError: typeof UnsupportedProtocolError;
303304

304305
/**
305306
An error to be thrown when the request is aborted due to a timeout.
306-
Includes an `event` and `timings` property.
307+
Includes an `event` and `timings` property. Contains a `code` property with `ETIMEDOUT`.
307308
*/
308309
TimeoutError: typeof TimeoutError;
309310

310311
/**
311312
An error to be thrown when the request body is a stream and an error occurs while reading from that stream.
313+
Contains a `code` property with `ERR_UPLOAD` or a more specific failure code.
312314
*/
313315
UploadError: typeof UploadError;
314316

315317
/**
316-
An error to be thrown when the request is aborted with `.cancel()`.
318+
An error to be thrown when the request is aborted with `.cancel()`. Contains a `code` property with `ERR_CANCELED`.
317319
*/
318320
CancelError: typeof CancelError;
319321

test/cache.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ test('cache error throws `got.CacheError`', withServer, async (t, server, got) =
120120
const cache = {};
121121

122122
// @ts-expect-error Error tests
123-
await t.throwsAsync(got({cache}), {instanceOf: got.CacheError});
123+
await t.throwsAsync(got({cache}), {
124+
instanceOf: got.CacheError,
125+
code: 'ERR_CACHE_ACCESS'
126+
});
124127
});
125128

126129
test('doesn\'t cache response when received HTTP error', withServer, async (t, server, got) => {

test/cancel.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ test.serial('does not retry after cancelation', withServerAndFakeTimers, async (
7979
gotPromise.cancel();
8080
});
8181

82-
await t.throwsAsync(gotPromise, {instanceOf: CancelError});
82+
await t.throwsAsync(gotPromise, {
83+
instanceOf: CancelError,
84+
code: 'ERR_CANCELED'
85+
});
8386
await t.notThrowsAsync(promise, 'Request finished instead of aborting.');
8487
});
8588

@@ -102,7 +105,10 @@ test.serial('cleans up request timeouts', withServer, async (t, server, got) =>
102105
}
103106
});
104107

105-
await t.throwsAsync(gotPromise, {instanceOf: CancelError});
108+
await t.throwsAsync(gotPromise, {
109+
instanceOf: CancelError,
110+
code: 'ERR_CANCELED'
111+
});
106112

107113
// Wait for unhandled errors
108114
await delay(40);
@@ -124,7 +130,10 @@ test.serial('cancels in-progress request', withServerAndFakeTimers, async (t, se
124130
body.push(null);
125131
});
126132

127-
await t.throwsAsync(gotPromise, {instanceOf: CancelError});
133+
await t.throwsAsync(gotPromise, {
134+
instanceOf: CancelError,
135+
code: 'ERR_CANCELED'
136+
});
128137
await t.notThrowsAsync(promise, 'Request finished instead of aborting.');
129138
});
130139

@@ -144,7 +153,10 @@ test.serial('cancels in-progress request with timeout', withServerAndFakeTimers,
144153
body.push(null);
145154
});
146155

147-
await t.throwsAsync(gotPromise, {instanceOf: CancelError});
156+
await t.throwsAsync(gotPromise, {
157+
instanceOf: CancelError,
158+
code: 'ERR_CANCELED'
159+
});
148160
await t.notThrowsAsync(promise, 'Request finished instead of aborting.');
149161
});
150162

@@ -220,7 +232,10 @@ test.serial('throws on incomplete (canceled) response - promise #2', withServerA
220232
promise.cancel();
221233
});
222234

223-
await t.throwsAsync(promise, {instanceOf: got.CancelError});
235+
await t.throwsAsync(promise, {
236+
instanceOf: got.CancelError,
237+
code: 'ERR_CANCELED'
238+
});
224239
});
225240

226241
test.serial('throws on incomplete (canceled) response - stream', withServerAndFakeTimers, async (t, server, got, clock) => {
@@ -250,7 +265,10 @@ test('throws when canceling cached request', withServer, async (t, server, got)
250265
promise.cancel();
251266
});
252267

253-
await t.throwsAsync(promise, {instanceOf: got.CancelError});
268+
await t.throwsAsync(promise, {
269+
instanceOf: got.CancelError,
270+
code: 'ERR_CANCELED'
271+
});
254272
});
255273

256274
test('throws when canceling cached request #2', withServer, async (t, server, got) => {
@@ -265,5 +283,8 @@ test('throws when canceling cached request #2', withServer, async (t, server, go
265283
const promise = got({cache});
266284
promise.cancel();
267285

268-
await t.throwsAsync(promise, {instanceOf: got.CancelError});
286+
await t.throwsAsync(promise, {
287+
instanceOf: got.CancelError,
288+
code: 'ERR_CANCELED'
289+
});
269290
});

test/error.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test('properties', withServer, async (t, server, got) => {
2828
// Class fields will always be initialized, even though they are undefined
2929
// A test to check for undefined is in place below
3030
// t.false({}.hasOwnProperty.call(error, 'code'));
31-
t.is(error.code, undefined);
31+
t.is(error.code, 'ERR_NON_2XX_3XX_RESPONSE');
3232
t.is(error.message, 'Response code 404 (Not Found)');
3333
t.deepEqual(error.options.url, url);
3434
t.is(error.response.headers.connection, 'close');
@@ -41,6 +41,7 @@ test('catches dns errors', async t => {
4141
t.regex(error.message, /ENOTFOUND|EAI_AGAIN/);
4242
t.is(error.options.url.host, 'doesntexist');
4343
t.is(error.options.method, 'GET');
44+
t.is(error.code, 'ENOTFOUND');
4445
});
4546

4647
test('`options.body` form error message', async t => {
@@ -130,6 +131,7 @@ test('`http.request` error', async t => {
130131
}
131132
}), {
132133
instanceOf: got.RequestError,
134+
code: 'ERR_GOT_REQUEST_ERROR',
133135
message: 'The header content contains invalid characters'
134136
});
135137
});
@@ -215,7 +217,8 @@ test('promise does not hang on timeout on HTTP error', withServer, async (t, ser
215217
await t.throwsAsync(got({
216218
timeout: 100
217219
}), {
218-
instanceOf: TimeoutError
220+
instanceOf: TimeoutError,
221+
code: 'ETIMEDOUT'
219222
});
220223
});
221224

test/http.ts

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ test('doesn\'t throw if `options.throwHttpErrors` is false', withServer, async (
9292
test('invalid protocol throws', async t => {
9393
await t.throwsAsync(got('c:/nope.com').json(), {
9494
instanceOf: UnsupportedProtocolError,
95+
code: 'ERR_UNSUPPORTED_PROTOCOL',
9596
message: 'Unsupported protocol "c:"'
9697
});
9798
});
@@ -225,6 +226,7 @@ test('throws an error if the server aborted the request', withServer, async (t,
225226
});
226227

227228
const error = await t.throwsAsync<ReadError>(got(''), {
229+
code: 'ECONNRESET',
228230
message: 'The server aborted pending request'
229231
});
230232

test/post.ts

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ test('throws on upload error', withServer, async (t, server, got) => {
354354
}
355355
})), {
356356
instanceOf: UploadError,
357+
code: 'ERR_UPLOAD',
357358
message
358359
});
359360
});

test/promise.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,14 @@ test('promise.json() can be called before a file stream body is open', withServe
7171

7272
const promise = got({body});
7373
const checks = [
74-
t.throwsAsync(promise, {instanceOf: CancelError}),
75-
t.throwsAsync(promise.json(), {instanceOf: CancelError})
74+
t.throwsAsync(promise, {
75+
instanceOf: CancelError,
76+
code: 'ERR_CANCELED'
77+
}),
78+
t.throwsAsync(promise.json(), {
79+
instanceOf: CancelError,
80+
code: 'ERR_CANCELED'
81+
})
7682
];
7783

7884
promise.cancel();

0 commit comments

Comments
 (0)