forked from Commitlabs-Org/Commitlabs-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
372 lines (331 loc) · 10.2 KB
/
Copy patherrors.ts
File metadata and controls
372 lines (331 loc) · 10.2 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* Error handling module.
*
* Defines typed error classes that correspond to HTTP status codes.
* Each error class maps to a definition in the centralized error code registry.
*
* @see errorCodes.ts for the centralized error code registry and documentation
*/
import { ERROR_CODE_REGISTRY } from "./errorCodes";
// ─── Base API error ───────────────────────────────────────────────────────────
export class ApiError extends Error {
constructor(
public readonly message: string,
public readonly code: string,
public readonly statusCode: number,
public readonly details?: unknown,
public readonly retryAfterSeconds?: number,
) {
super(message);
this.name = "ApiError";
}
}
// ─── Named subclasses ─────────────────────────────────────────────────────────
/** 400 — malformed or invalid request input. */
export class BadRequestError extends ApiError {
constructor(message = "Bad request.", details?: unknown) {
super(message, "BAD_REQUEST", 400, details);
this.name = "BadRequestError";
}
}
/** 400 — request body / query params failed validation. */
export class ValidationError extends ApiError {
constructor(message = "Invalid request data.", details?: unknown) {
super(message, "VALIDATION_ERROR", 400, details);
this.name = "ValidationError";
}
}
/** 401 — missing or invalid authentication credentials. */
export class UnauthorizedError extends ApiError {
constructor(message = "Authentication required.", details?: unknown) {
super(message, "UNAUTHORIZED", 401, details);
this.name = "UnauthorizedError";
}
}
/** 403 — authenticated but not permitted to perform the action. */
export class ForbiddenError extends ApiError {
constructor(
message = "You do not have permission to perform this action.",
details?: unknown,
) {
super(message, "FORBIDDEN", 403, details);
this.name = "ForbiddenError";
}
}
/** 403 — CSRF token missing, invalid, or cross-site request when using cookie session. */
export class CsrfValidationError extends ApiError {
constructor(message = 'Invalid or missing CSRF token.', details?: unknown) {
super(message, 'CSRF_INVALID', 403, details);
this.name = 'CsrfValidationError';
}
}
/** 404 — requested resource does not exist. */
export class NotFoundError extends ApiError {
constructor(resource = "Resource", details?: unknown) {
super(`${resource} not found.`, "NOT_FOUND", 404, details);
this.name = "NotFoundError";
}
}
/** 409 — request conflicts with current state (e.g. duplicate). */
export class ConflictError extends ApiError {
constructor(message = "A conflict occurred.", details?: unknown) {
super(message, "CONFLICT", 409, details);
this.name = "ConflictError";
}
}
/** 413 — request entity is larger than the server is willing to process. */
export class PayloadTooLargeError extends ApiError {
constructor(message = "Request body is too large.", details?: unknown) {
super(message, "PAYLOAD_TOO_LARGE", 413, details);
this.name = "PayloadTooLargeError";
}
}
/** 429 — client has exceeded the allowed request rate. */
export class TooManyRequestsError extends ApiError {
constructor(
message = "Too many requests. Please try again later.",
details?: unknown,
retryAfterSeconds = 60,
) {
super(message, "TOO_MANY_REQUESTS", 429, details, retryAfterSeconds);
this.name = "TooManyRequestsError";
}
}
/** 503 — service is temporarily unavailable. */
export class ServiceUnavailableError extends ApiError {
constructor(
message = "The service is temporarily unavailable. Please try again later.",
details?: unknown,
retryAfterSeconds = 30,
) {
super(message, "SERVICE_UNAVAILABLE", 503, details, retryAfterSeconds);
this.name = "ServiceUnavailableError";
}
}
/** 500 — unexpected server-side failure. */
export class InternalError extends ApiError {
constructor(
message = "An unexpected error occurred. Please try again later.",
details?: unknown,
) {
super(message, "INTERNAL_ERROR", 500, details);
this.name = "InternalError";
}
}
// ─── HTTP status → error code mapping ───────────────────────────────────────
/**
* Map of HTTP status codes to their canonical error code strings.
*
* @deprecated Use ERROR_CODE_REGISTRY from errorCodes.ts for detailed error
* documentation including meaning, client handling, and retriable status.
*
* @see ERROR_CODE_REGISTRY
*/
export const HTTP_ERROR_CODES: Record<number, string> = {
400: "BAD_REQUEST",
401: "UNAUTHORIZED",
403: "FORBIDDEN",
404: "NOT_FOUND",
409: "CONFLICT",
413: "PAYLOAD_TOO_LARGE",
422: "UNPROCESSABLE_ENTITY",
429: "TOO_MANY_REQUESTS",
500: "INTERNAL_ERROR",
502: "BAD_GATEWAY",
503: "SERVICE_UNAVAILABLE",
504: "GATEWAY_TIMEOUT",
};
// ─── Legacy BackendError (kept for backward compatibility) ────────────────────
export type BackendErrorCode =
| "BAD_REQUEST"
| "NOT_MATURED"
| "VALIDATION_ERROR"
| "UNAUTHORIZED"
| "FORBIDDEN"
| "NOT_FOUND"
| "CONFLICT"
| "PAYLOAD_TOO_LARGE"
| "TOO_MANY_REQUESTS"
| "SERVICE_UNAVAILABLE"
| "GATEWAY_TIMEOUT"
| "BLOCKCHAIN_UNAVAILABLE"
| "BLOCKCHAIN_CALL_FAILED"
| "INTERNAL_ERROR";
export interface BackendErrorOptions {
code: BackendErrorCode;
message: string;
status: number;
details?: Record<string, unknown>;
cause?: unknown;
}
export class BackendError extends Error {
readonly code: BackendErrorCode;
readonly status: number;
readonly details?: Record<string, unknown>;
readonly cause?: unknown;
constructor(options: BackendErrorOptions) {
super(options.message);
this.name = "BackendError";
this.code = options.code;
this.status = options.status;
this.details = options.details;
this.cause = options.cause;
}
}
export interface BackendErrorResponseBody {
error: {
code: BackendErrorCode;
message: string;
details?: Record<string, unknown>;
};
}
export function isBackendError(value: unknown): value is BackendError {
return value instanceof BackendError;
}
function asRecord(value: unknown): Record<string, unknown> {
return value && typeof value === "object"
? (value as Record<string, unknown>)
: {};
}
function isRetryableStatus(status: number): boolean {
return [429, 503, 504].includes(status);
}
function classifyBackendError(
error: unknown,
):
| {
code: BackendErrorCode;
status: number;
message: string;
retryable: boolean;
}
| undefined {
const errMessage = error instanceof Error ? error.message : String(error);
const errStr = errMessage.toLowerCase();
if (
errStr.includes("timeout") ||
errStr.includes("deadline") ||
errStr.includes("timed out")
) {
return {
code: "GATEWAY_TIMEOUT",
status: 504,
message:
"The blockchain operation timed out. It may still be processed later.",
retryable: true,
};
}
if (
errStr.includes("429") ||
errStr.includes("rate limit") ||
errStr.includes("too many requests")
) {
return {
code: "TOO_MANY_REQUESTS",
status: 429,
message: "Rate limit exceeded for blockchain calls. Please try again later.",
retryable: true,
};
}
if (
errStr.includes("503") ||
errStr.includes("service unavailable") ||
errStr.includes("temporarily unavailable")
) {
return {
code: "SERVICE_UNAVAILABLE",
status: 503,
message: "Blockchain service is temporarily unavailable. Please try again later.",
retryable: true,
};
}
if (errStr.includes("not found") || errStr.includes("404")) {
return {
code: "NOT_FOUND",
status: 404,
message: "The requested resource was not found on the blockchain.",
retryable: false,
};
}
if (
errStr.includes("insufficient") ||
errStr.includes("invalid") ||
errStr.includes("malformed")
) {
return {
code: "VALIDATION_ERROR",
status: 400,
message:
"The transaction was rejected due to invalid parameters or state.",
retryable: false,
};
}
return undefined;
}
export function normalizeBackendError(
error: unknown,
fallback: Omit<BackendErrorOptions, "cause">,
): BackendError {
if (isBackendError(error)) {
const details = {
...asRecord(error.details),
...asRecord(fallback.details),
};
const retryable =
asRecord(error.details).retryable === true ||
isRetryableStatus(error.status);
return new BackendError({
code: error.code,
message: error.message,
status: error.status,
details: {
...details,
retryable,
},
cause: error,
});
}
const classified =
fallback.code === "BLOCKCHAIN_CALL_FAILED"
? classifyBackendError(error)
: undefined;
const status = classified?.status ?? fallback.status;
const code = classified?.code ?? fallback.code;
const message = classified?.message ?? fallback.message;
const retryable = classified?.retryable ?? isRetryableStatus(fallback.status);
return new BackendError({
code,
message,
status,
details: {
...asRecord(fallback.details),
retryable,
},
cause: error,
});
}
export function toBackendErrorResponse(
error: BackendError,
): BackendErrorResponseBody {
return {
error: {
code: error.code,
message: error.message,
details: error.details,
},
};
}
// ─── Error code registry exports ──────────────────────────────────────────────
/**
* Re-export error code registry utilities for easy access throughout the application.
*
* @see errorCodes.ts for full registry and documentation
*/
export {
ERROR_CODE_REGISTRY,
getErrorCodeDefinition,
getErrorCodesByStatus,
validateErrorCodeRegistry,
type ErrorCodeDefinition,
type RegisteredErrorCode,
} from "./errorCodes";