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 |
|
19 | interface AxiosErrorDetails {
|
20 | originalError: Error;
|
21 | code?: string;
|
22 | statusCode?: number;
|
23 | statusMessage?: string;
|
24 | }
|
25 |
|
26 | export class SignatureValidationFailed extends Error {
|
27 | public signature?: string;
|
28 |
|
29 | constructor(message: Message, { signature }: ErrorDetails = {}) {
|
30 | super(message);
|
31 | this.name = this.constructor.name;
|
32 |
|
33 | Object.assign(this, { signature });
|
34 | }
|
35 | }
|
36 |
|
37 | export class JSONParseError extends Error {
|
38 | public raw: any;
|
39 |
|
40 | constructor(message: Message, { raw }: ErrorDetails = {}) {
|
41 | super(message);
|
42 | this.name = this.constructor.name;
|
43 |
|
44 | Object.assign(this, { raw });
|
45 | }
|
46 | }
|
47 |
|
48 |
|
49 | export class RequestError extends Error {
|
50 | public code: string;
|
51 |
|
52 | private originalError: Error;
|
53 |
|
54 | constructor(message: Message, { code, originalError }: AxiosErrorDetails) {
|
55 | super(message);
|
56 | this.name = this.constructor.name;
|
57 |
|
58 | Object.assign(this, { code, originalError });
|
59 | }
|
60 | }
|
61 |
|
62 | export class ReadError extends Error {
|
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 });
|
70 | }
|
71 | }
|
72 |
|
73 | export class HTTPError extends Error {
|
74 | public statusCode: number;
|
75 |
|
76 | public statusMessage: string;
|
77 |
|
78 | public originalError: any;
|
79 |
|
80 | constructor(
|
81 | message: Message,
|
82 | { statusCode, statusMessage, originalError }: AxiosErrorDetails,
|
83 | ) {
|
84 | super(message);
|
85 | this.name = this.constructor.name;
|
86 |
|
87 | Object.assign(this, { statusCode, statusMessage, originalError });
|
88 | }
|
89 | }
|
90 |
|
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 |
|
100 | constructor(
|
101 | message: Message,
|
102 | { status, statusText, headers, body }: FetchErrorDetails,
|
103 | ) {
|
104 | super(message);
|
105 | this.name = this.constructor.name;
|
106 |
|
107 | Object.assign(this, { status, statusText, headers, body });
|
108 | }
|
109 | }
|