-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy patherrors.ts
65 lines (57 loc) · 2.64 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
import { AxiosResponse } from 'axios';
import { APIErrorResponse } from './types';
export const APIErrorCodes: Record<string, { name: string; retryable: boolean }> = {
'-1': { name: 'InternalSystemError', retryable: true },
'2': { name: 'AccessKeyError', retryable: false },
'3': { name: 'AuthenticationFailedError', retryable: true },
'4': { name: 'InputError', retryable: false },
'6': { name: 'DuplicateUsernameError', retryable: false },
'9': { name: 'RateLimitError', retryable: true },
'16': { name: 'DoesNotExistError', retryable: false },
'17': { name: 'NotAllowedError', retryable: false },
'18': { name: 'EventNotSupportedError', retryable: false },
'19': { name: 'ChannelFeatureNotSupportedError', retryable: false },
'20': { name: 'MessageTooLongError', retryable: false },
'21': { name: 'MultipleNestingLevelError', retryable: false },
'22': { name: 'PayloadTooBigError', retryable: false },
'23': { name: 'RequestTimeoutError', retryable: true },
'24': { name: 'MaxHeaderSizeExceededError', retryable: false },
'40': { name: 'AuthErrorTokenExpired', retryable: false },
'41': { name: 'AuthErrorTokenNotValidYet', retryable: false },
'42': { name: 'AuthErrorTokenUsedBeforeIssuedAt', retryable: false },
'43': { name: 'AuthErrorTokenSignatureInvalid', retryable: false },
'44': { name: 'CustomCommandEndpointMissingError', retryable: false },
'45': { name: 'CustomCommandEndpointCallError', retryable: true },
'46': { name: 'ConnectionIDNotFoundError', retryable: false },
'60': { name: 'CoolDownError', retryable: true },
'69': { name: 'ErrWrongRegion', retryable: false },
'70': { name: 'ErrQueryChannelPermissions', retryable: false },
'71': { name: 'ErrTooManyConnections', retryable: true },
'99': { name: 'AppSuspendedError', retryable: false },
};
type APIError = Error & { code: number; isWSFailure?: boolean };
export function isAPIError(error: Error): error is APIError {
return (error as APIError).code !== undefined;
}
export function isErrorRetryable(error: APIError) {
if (!error.code) return false;
const err = APIErrorCodes[`${error.code}`];
if (!err) return false;
return err.retryable;
}
export function isConnectionIDError(error: APIError) {
return error.code === 46; // ConnectionIDNotFoundError
}
export function isWSFailure(err: APIError): boolean {
if (typeof err.isWSFailure === 'boolean') {
return err.isWSFailure;
}
try {
return JSON.parse(err.message).isWSFailure;
} catch (_) {
return false;
}
}
export function isErrorResponse(res: AxiosResponse<unknown>): res is AxiosResponse<APIErrorResponse> {
return !res.status || res.status < 200 || 300 <= res.status;
}