/
errors.ts
125 lines (102 loc) 路 3.58 KB
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import is from '@sindresorhus/is';
import {Timings} from '@szmarczak/http-timer';
import {TimeoutError as TimedOutError} from './utils/timed-out';
import {ErrorCode, Response, NormalizedOptions} from './utils/types';
export class GotError extends Error {
code?: ErrorCode;
stack!: string;
declare readonly options: NormalizedOptions;
constructor(message: string, error: Partial<Error & {code?: ErrorCode}>, options: NormalizedOptions) {
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = 'GotError';
if (!is.undefined(error.code)) {
this.code = error.code;
}
Object.defineProperty(this, 'options', {
// This fails because of TS 3.7.2 useDefineForClassFields
// Ref: https://github.com/microsoft/TypeScript/issues/34972
enumerable: false,
value: options
});
// Recover the original stacktrace
if (!is.undefined(error.stack)) {
const indexOfMessage = this.stack.indexOf(this.message) + this.message.length;
const thisStackTrace = this.stack.slice(indexOfMessage).split('\n').reverse();
const errorStackTrace = error.stack.slice(error.stack.indexOf(error.message!) + error.message!.length).split('\n').reverse();
// Remove duplicated traces
while (errorStackTrace.length !== 0 && errorStackTrace[0] === thisStackTrace[0]) {
thisStackTrace.shift();
}
this.stack = `${this.stack.slice(0, indexOfMessage)}${thisStackTrace.reverse().join('\n')}${errorStackTrace.reverse().join('\n')}`;
}
}
}
export class CacheError extends GotError {
constructor(error: Error, options: NormalizedOptions) {
super(error.message, error, options);
this.name = 'CacheError';
}
}
export class RequestError extends GotError {
constructor(error: Error, options: NormalizedOptions) {
super(error.message, error, options);
this.name = 'RequestError';
}
}
export class ReadError extends GotError {
constructor(error: Error, options: NormalizedOptions) {
super(error.message, error, options);
this.name = 'ReadError';
}
}
export class ParseError extends GotError {
declare readonly response: Response;
constructor(error: Error, response: Response, options: NormalizedOptions) {
super(`${error.message} in "${options.url.toString()}"`, error, options);
this.name = 'ParseError';
Object.defineProperty(this, 'response', {
enumerable: false,
value: response
});
}
}
export class HTTPError extends GotError {
declare readonly response: Response;
constructor(response: Response, options: NormalizedOptions) {
super(`Response code ${response.statusCode} (${response.statusMessage ?? ''})`, {}, options);
this.name = 'HTTPError';
Object.defineProperty(this, 'response', {
enumerable: false,
value: response
});
}
}
export class MaxRedirectsError extends GotError {
declare readonly response: Response;
constructor(response: Response, maxRedirects: number, options: NormalizedOptions) {
super(`Redirected ${maxRedirects} times. Aborting.`, {}, options);
this.name = 'MaxRedirectsError';
Object.defineProperty(this, 'response', {
enumerable: false,
value: response
});
}
}
export class UnsupportedProtocolError extends GotError {
constructor(options: NormalizedOptions) {
super(`Unsupported protocol "${options.url.protocol}"`, {}, options);
this.name = 'UnsupportedProtocolError';
}
}
export class TimeoutError extends GotError {
timings: Timings;
event: string;
constructor(error: TimedOutError, timings: Timings, options: NormalizedOptions) {
super(error.message, error, options);
this.name = 'TimeoutError';
this.event = error.event;
this.timings = timings;
}
}
export {CancelError} from 'p-cancelable';