|
| 1 | +type Message = string; |
| 2 | + |
| 3 | +interface Status { |
| 4 | + status: number; |
| 5 | + statusText: string; |
| 6 | +} |
| 7 | + |
| 8 | +interface ErrorDetails { |
| 9 | + signature?: string; |
| 10 | + raw?: any; |
| 11 | +} |
| 12 | + |
| 13 | +interface FetchErrorDetails extends Status { |
| 14 | + headers: Headers; |
| 15 | + body: string; |
| 16 | +} |
| 17 | + |
| 18 | +// Deprecated |
| 19 | +interface AxiosErrorDetails { |
| 20 | + originalError: Error; |
| 21 | + code?: string; |
| 22 | + statusCode?: number; |
| 23 | + statusMessage?: string; |
| 24 | +} |
| 25 | + |
1 | 26 | export class SignatureValidationFailed extends Error { |
2 | | - constructor( |
3 | | - message: string, |
4 | | - public signature?: string, |
5 | | - ) { |
| 27 | + public signature?: string; |
| 28 | + |
| 29 | + constructor(message: Message, { signature }: ErrorDetails = {}) { |
6 | 30 | super(message); |
| 31 | + this.name = this.constructor.name; |
| 32 | + |
| 33 | + Object.assign(this, { signature }); |
7 | 34 | } |
8 | 35 | } |
9 | 36 |
|
10 | 37 | export class JSONParseError extends Error { |
11 | | - constructor( |
12 | | - message: string, |
13 | | - public raw: any, |
14 | | - ) { |
| 38 | + public raw: any; |
| 39 | + |
| 40 | + constructor(message: Message, { raw }: ErrorDetails = {}) { |
15 | 41 | super(message); |
| 42 | + this.name = this.constructor.name; |
| 43 | + |
| 44 | + Object.assign(this, { raw }); |
16 | 45 | } |
17 | 46 | } |
18 | 47 |
|
| 48 | +/* Deprecated */ |
19 | 49 | export class RequestError extends Error { |
20 | | - constructor( |
21 | | - message: string, |
22 | | - public code: string, |
23 | | - private originalError: Error, |
24 | | - ) { |
| 50 | + public code: string; |
| 51 | + |
| 52 | + private originalError: Error; |
| 53 | + |
| 54 | + constructor(message: Message, { code, originalError }: AxiosErrorDetails) { |
25 | 55 | super(message); |
| 56 | + this.name = this.constructor.name; |
| 57 | + |
| 58 | + Object.assign(this, { code, originalError }); |
26 | 59 | } |
27 | 60 | } |
28 | 61 |
|
29 | 62 | export class ReadError extends Error { |
30 | | - constructor(private originalError: Error) { |
31 | | - super(originalError.message); |
| 63 | + public originalError: Error; |
| 64 | + |
| 65 | + constructor(message: Message, { originalError }: AxiosErrorDetails) { |
| 66 | + super(message); |
| 67 | + this.name = this.constructor.name; |
| 68 | + |
| 69 | + Object.assign(this, { originalError }); |
32 | 70 | } |
33 | 71 | } |
34 | 72 |
|
35 | 73 | export class HTTPError extends Error { |
| 74 | + public statusCode: number; |
| 75 | + |
| 76 | + public statusMessage: string; |
| 77 | + |
| 78 | + public originalError: any; |
| 79 | + |
36 | 80 | constructor( |
37 | | - message: string, |
38 | | - public statusCode: number, |
39 | | - public statusMessage: string, |
40 | | - public originalError: any, |
| 81 | + message: Message, |
| 82 | + { statusCode, statusMessage, originalError }: AxiosErrorDetails, |
41 | 83 | ) { |
42 | 84 | super(message); |
| 85 | + this.name = this.constructor.name; |
| 86 | + |
| 87 | + Object.assign(this, { statusCode, statusMessage, originalError }); |
43 | 88 | } |
44 | 89 | } |
45 | 90 |
|
46 | 91 | export class HTTPFetchError extends Error { |
| 92 | + public status: number; |
| 93 | + |
| 94 | + public statusText: string; |
| 95 | + |
| 96 | + public headers: Headers; |
| 97 | + |
| 98 | + public body: string; |
| 99 | + |
47 | 100 | constructor( |
48 | | - public statusCode: number, |
49 | | - public statusMessage: string, |
50 | | - public headers: Headers, |
51 | | - public body: string, |
| 101 | + message: Message, |
| 102 | + { status, statusText, headers, body }: FetchErrorDetails, |
52 | 103 | ) { |
53 | | - super(`${statusCode} - ${statusMessage}`); |
| 104 | + super(message); |
| 105 | + this.name = this.constructor.name; |
| 106 | + |
| 107 | + Object.assign(this, { status, statusText, headers, body }); |
54 | 108 | } |
55 | 109 | } |
0 commit comments