-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.ts
115 lines (101 loc) · 2.96 KB
/
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Enumeration of the different types of errors that can be thrown by
* HttpUtils.
*/
enum HttpUtilsErrorType {
StatusCodeNotAccepted,
NoBodyInResponse,
InvalidStatusCodeRange,
InvalidHeaders,
GenericFetchError,
IllegalParameters,
ConnectionError,
TimeOutError,
UnauthorizedError,
CredentialIssue,
OAuth2TokenError,
InvalidCronExpression,
}
/**
* Extension of the Error class specifically designed for HttpFetch.
*/
export class HttpUtilsError extends Error {
readonly type: HttpUtilsErrorType;
constructor(message: string, type: HttpUtilsErrorType) {
super(message);
this.name = "HttpUtilsError";
this.type = type;
}
static statusCodeNotAccepted(statusCode: number) {
return new HttpUtilsError(
`Status code ${statusCode} not accepted`,
HttpUtilsErrorType.StatusCodeNotAccepted,
);
}
static noBodyInResponse() {
return new HttpUtilsError(
"No body in response",
HttpUtilsErrorType.NoBodyInResponse,
);
}
static invalidStatusCodeRange() {
return new HttpUtilsError(
"Invalid status code range",
HttpUtilsErrorType.InvalidStatusCodeRange,
);
}
static invalidHeaders() {
return new HttpUtilsError(
"Invalid headers",
HttpUtilsErrorType.InvalidHeaders,
);
}
static genericFetchError(error: Error) {
return new HttpUtilsError(
`Generic fetch error: ${error.message}`,
HttpUtilsErrorType.GenericFetchError,
);
}
static illegalParameters(info: string | null = null) {
return new HttpUtilsError(
info ?? "Illegal parameters",
HttpUtilsErrorType.IllegalParameters,
);
}
static connectionError() {
return new HttpUtilsError(
"Connection error",
HttpUtilsErrorType.ConnectionError,
);
}
static timeOutError(ms: number | null) {
return new HttpUtilsError(
`Request exceeded time limit of ${ms} ms`,
HttpUtilsErrorType.TimeOutError,
);
}
static unauthorizedError() {
return new HttpUtilsError(
"Unauthorized",
HttpUtilsErrorType.UnauthorizedError,
);
}
static credentialIssue() {
return new HttpUtilsError(
"Credentials are invalid or have insufficient access",
HttpUtilsErrorType.CredentialIssue,
);
}
static oAuth2TokenError(code: number) {
return new HttpUtilsError(
`An issue occurred while retrieving the OAuth2 token. Response with status code ${code}.`,
HttpUtilsErrorType.OAuth2TokenError,
);
}
static invalidCronExpression() {
return new HttpUtilsError(
"The provided cron job expression is invalid",
HttpUtilsErrorType.InvalidCronExpression,
);
}
}