/
errors.ts
50 lines (43 loc) · 1.04 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
import { AxiosResponse } from 'axios';
export class ApiError extends Error {
private resp: AxiosResponse;
constructor(resp: AxiosResponse) {
super();
this.resp = resp;
// Set the prototype explicitly.
Object.setPrototypeOf(this, ApiError.prototype);
}
get isAuthorizationError(): boolean {
return this.resp.status == 401 || this.resp.status === 403;
}
get message(): string {
try {
if (this.resp.data.Message) {
return this.resp.data.Message;
}
} catch (e) {}
return this.resp.statusText;
}
toString() {
const message = this.message;
if (this.isAuthorizationError && !message) {
return 'Authorization Failed';
}
let str = message;
if (this.resp.request) {
str = `${message}
METHOD: ${this.resp.request.method}
HOST: ${this.resp.config.baseURL}
URL: ${this.resp.request.path}
`;
}
return str;
}
}
export const AuthorizationError = new ApiError({
config: {},
data: null,
headers: null,
statusText: 'Authorization Failed',
status: 401,
});