This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy patherrors.ts
211 lines (184 loc) · 4.81 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import EmberError from '@ember/error';
export class AjaxError extends EmberError {
payload: any;
status: number;
constructor(payload: any, message = 'Ajax operation failed', status: number) {
super(message);
this.payload = payload;
this.status = status;
}
}
export class InvalidError extends AjaxError {
constructor(payload: any) {
super(payload, 'Request was rejected because it was invalid', 422);
}
}
export class UnauthorizedError extends AjaxError {
constructor(payload: any) {
super(payload, 'Ajax authorization failed', 401);
}
}
export class ForbiddenError extends AjaxError {
constructor(payload: any) {
super(
payload,
'Request was rejected because user is not permitted to perform this operation.',
403
);
}
}
export class BadRequestError extends AjaxError {
constructor(payload: any) {
super(payload, 'Request was formatted incorrectly.', 400);
}
}
export class NotFoundError extends AjaxError {
constructor(payload: any) {
super(payload, 'Resource was not found.', 404);
}
}
export class GoneError extends AjaxError {
constructor(payload: any) {
super(payload, 'Resource is no longer available.', 410);
}
}
export class TimeoutError extends AjaxError {
constructor() {
super(null, 'The ajax operation timed out', -1);
}
}
export class AbortError extends AjaxError {
constructor() {
super(null, 'The ajax operation was aborted', 0);
}
}
export class ConflictError extends AjaxError {
constructor(payload: any) {
super(payload, 'The ajax operation failed due to a conflict', 409);
}
}
export class ServerError extends AjaxError {
constructor(payload: any, status: number) {
super(payload, 'Request was rejected due to server error', status);
}
}
/**
* Checks if the given error is or inherits from AjaxError
*/
export function isAjaxError(error: any): error is AjaxError {
return error instanceof AjaxError;
}
/**
* Checks if the given status code or AjaxError object represents an
* unauthorized request error
*/
export function isUnauthorizedError(error: AjaxError | number): boolean {
if (isAjaxError(error)) {
return error instanceof UnauthorizedError;
} else {
return error === 401;
}
}
/**
* Checks if the given status code or AjaxError object represents a forbidden
* request error
*/
export function isForbiddenError(error: AjaxError | number): boolean {
if (isAjaxError(error)) {
return error instanceof ForbiddenError;
} else {
return error === 403;
}
}
/**
* Checks if the given status code or AjaxError object represents an invalid
* request error
*/
export function isInvalidError(error: AjaxError | number): boolean {
if (isAjaxError(error)) {
return error instanceof InvalidError;
} else {
return error === 422;
}
}
/**
* Checks if the given status code or AjaxError object represents a bad request
* error
*/
export function isBadRequestError(error: AjaxError | number): boolean {
if (isAjaxError(error)) {
return error instanceof BadRequestError;
} else {
return error === 400;
}
}
/**
* Checks if the given status code or AjaxError object represents a "not found"
* error
*/
export function isNotFoundError(error: AjaxError | number): boolean {
if (isAjaxError(error)) {
return error instanceof NotFoundError;
} else {
return error === 404;
}
}
/**
* Checks if the given status code or AjaxError object represents a "gone"
* error
*/
export function isGoneError(error: AjaxError | number): boolean {
if (isAjaxError(error)) {
return error instanceof GoneError;
} else {
return error === 410;
}
}
/**
* Checks if the given object represents a "timeout" error
*/
export function isTimeoutError(error: any) {
return error instanceof TimeoutError;
}
/**
* Checks if the given status code or AjaxError object represents an
* "abort" error
*/
export function isAbortError(error: AjaxError | number): boolean {
if (isAjaxError(error)) {
return error instanceof AbortError;
} else {
return error === 0;
}
}
/**
* Checks if the given status code or AjaxError object represents a
* conflict error
*/
export function isConflictError(error: AjaxError | number): boolean {
if (isAjaxError(error)) {
return error instanceof ConflictError;
} else {
return error === 409;
}
}
/**
* Checks if the given status code or AjaxError object represents a server error
*/
export function isServerError(error: AjaxError | number): boolean {
if (isAjaxError(error)) {
return error instanceof ServerError;
} else {
return error >= 500 && error < 600;
}
}
/**
* Checks if the given status code represents a successful request
*/
export function isSuccess(status: number | string) {
let s = status;
if (typeof status === 'string') {
s = parseInt(status, 10);
}
return (s >= 200 && s < 300) || s === 304;
}