/
axios-extended-error.ts
71 lines (59 loc) · 2.26 KB
/
axios-extended-error.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
// dummy
class AxiosErrorLike<T = unknown, D = any> extends Error {
config!: any;
code?: string;
request?: any;
response?: any;
isAxiosError!: boolean;
status?: string;
constructor(message?: string, code?: string, config?: any, request?: any, response?: any) {
super();
}
}
let axiosErrorLike = AxiosErrorLike;
let transformError = (e: AxiosErrorLike) => e;
export declare class RequestUriTooLongException {}
export declare class TooManyRequestException {}
try {
const axios = require('axios');
const nestjs = require('@nestjs/common');
const { HttpErrorByCode } = require('@nestjs/common/utils/http-error-by-code.util');
if (axios?.AxiosError) axiosErrorLike = axios.AxiosError;
class RequestUriTooLongException extends nestjs.HttpException {
constructor(objectOrError?: string | object | any, description?: string) {
super(nestjs.HttpException.createBody(objectOrError, description, 414), 414);
}
}
class TooManyRequestException extends nestjs.HttpException {
constructor(objectOrError?: string | object | any, description?: string) {
super(nestjs.HttpException.createBody(objectOrError, description, 429), 429);
}
}
transformError = (e: AxiosErrorLike) => {
const axiosErrorStatus = ((): number => {
if (e.code === 'ECONNABORTED' && e.message?.match(/timeout/i)) {
return nestjs.HttpStatus.REQUEST_TIMEOUT;
}
if (e.response?.status) {
return +e.response?.status;
}
if (e.status) {
return +e.status;
}
return nestjs.HttpStatus.INTERNAL_SERVER_ERROR;
})();
if (HttpErrorByCode && HttpErrorByCode[axiosErrorStatus]) return new HttpErrorByCode[axiosErrorStatus](e.message);
switch (axiosErrorStatus) {
case 414:
return new RequestUriTooLongException(e);
case 429:
return new TooManyRequestException(e);
}
return e;
};
} catch (e) {}
export const AxiosExceptionPipe = (e: unknown): unknown => {
if (!(e instanceof axiosErrorLike)) return e;
const error: AxiosErrorLike = e;
return transformError(error as AxiosErrorLike);
};