@vertz/errors 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1165 -490
- package/dist/index.js +618 -141
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -1,51 +1,3 @@
|
|
|
1
|
-
// src/result.ts
|
|
2
|
-
var ok = (data) => ({ ok: true, data });
|
|
3
|
-
var err = (error) => ({ ok: false, error });
|
|
4
|
-
function unwrap(result) {
|
|
5
|
-
if (result.ok) {
|
|
6
|
-
return result.data;
|
|
7
|
-
}
|
|
8
|
-
throw result.error;
|
|
9
|
-
}
|
|
10
|
-
function unwrapOr(result, defaultValue) {
|
|
11
|
-
if (result.ok) {
|
|
12
|
-
return result.data;
|
|
13
|
-
}
|
|
14
|
-
return defaultValue;
|
|
15
|
-
}
|
|
16
|
-
function map(result, fn) {
|
|
17
|
-
if (result.ok) {
|
|
18
|
-
return { ok: true, data: fn(result.data) };
|
|
19
|
-
}
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
function flatMap(result, fn) {
|
|
23
|
-
if (result.ok) {
|
|
24
|
-
return fn(result.data);
|
|
25
|
-
}
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
function match(result, handlers) {
|
|
29
|
-
return result.ok ? handlers.ok(result.data) : handlers.err(result.error);
|
|
30
|
-
}
|
|
31
|
-
function matchErr(result, handlers) {
|
|
32
|
-
if (result.ok) {
|
|
33
|
-
return handlers.ok(result.data);
|
|
34
|
-
}
|
|
35
|
-
const errorCode = result.error.code;
|
|
36
|
-
const handlersRecord = handlers;
|
|
37
|
-
const handler = handlersRecord[errorCode];
|
|
38
|
-
if (!handler) {
|
|
39
|
-
throw new Error(`Unhandled error code: ${errorCode}`);
|
|
40
|
-
}
|
|
41
|
-
return handler(result.error);
|
|
42
|
-
}
|
|
43
|
-
function isOk(result) {
|
|
44
|
-
return result.ok === true;
|
|
45
|
-
}
|
|
46
|
-
function isErr(result) {
|
|
47
|
-
return result.ok === false;
|
|
48
|
-
}
|
|
49
1
|
// src/app-error.ts
|
|
50
2
|
class AppError extends Error {
|
|
51
3
|
code;
|
|
@@ -61,70 +13,6 @@ class AppError extends Error {
|
|
|
61
13
|
};
|
|
62
14
|
}
|
|
63
15
|
}
|
|
64
|
-
// src/domain/schema.ts
|
|
65
|
-
function createValidationError(message, issues) {
|
|
66
|
-
return {
|
|
67
|
-
code: "VALIDATION_FAILED",
|
|
68
|
-
message,
|
|
69
|
-
issues
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
function isValidationError(error) {
|
|
73
|
-
return error.code === "VALIDATION_FAILED";
|
|
74
|
-
}
|
|
75
|
-
// src/domain/db.ts
|
|
76
|
-
function createNotFoundError(table, key) {
|
|
77
|
-
const keyStr = key ? JSON.stringify(key) : "";
|
|
78
|
-
return {
|
|
79
|
-
code: "NOT_FOUND",
|
|
80
|
-
message: `Record not found in ${table}${keyStr ? `: ${keyStr}` : ""}`,
|
|
81
|
-
table,
|
|
82
|
-
key
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
function isNotFoundError(error) {
|
|
86
|
-
return error.code === "NOT_FOUND";
|
|
87
|
-
}
|
|
88
|
-
function createUniqueViolation(message, options) {
|
|
89
|
-
return {
|
|
90
|
-
code: "UNIQUE_VIOLATION",
|
|
91
|
-
message,
|
|
92
|
-
...options
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
function isUniqueViolation(error) {
|
|
96
|
-
return error.code === "UNIQUE_VIOLATION";
|
|
97
|
-
}
|
|
98
|
-
function createFKViolation(message, options) {
|
|
99
|
-
return {
|
|
100
|
-
code: "FK_VIOLATION",
|
|
101
|
-
message,
|
|
102
|
-
...options
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
function isFKViolation(error) {
|
|
106
|
-
return error.code === "FK_VIOLATION";
|
|
107
|
-
}
|
|
108
|
-
function createNotNullViolation(message, options) {
|
|
109
|
-
return {
|
|
110
|
-
code: "NOT_NULL_VIOLATION",
|
|
111
|
-
message,
|
|
112
|
-
...options
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function isNotNullViolation(error) {
|
|
116
|
-
return error.code === "NOT_NULL_VIOLATION";
|
|
117
|
-
}
|
|
118
|
-
function createCheckViolation(message, options) {
|
|
119
|
-
return {
|
|
120
|
-
code: "CHECK_VIOLATION",
|
|
121
|
-
message,
|
|
122
|
-
...options
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
function isCheckViolation(error) {
|
|
126
|
-
return error.code === "CHECK_VIOLATION";
|
|
127
|
-
}
|
|
128
16
|
// src/domain/auth.ts
|
|
129
17
|
function createInvalidCredentialsError(message = "Invalid email or password") {
|
|
130
18
|
return {
|
|
@@ -174,54 +62,65 @@ function createRateLimitedError(message = "Too many attempts, please try again l
|
|
|
174
62
|
function isRateLimitedError(error) {
|
|
175
63
|
return error.code === "RATE_LIMITED";
|
|
176
64
|
}
|
|
65
|
+
function createAuthValidationError(message, field, constraint) {
|
|
66
|
+
return {
|
|
67
|
+
code: "AUTH_VALIDATION_ERROR",
|
|
68
|
+
message,
|
|
69
|
+
field,
|
|
70
|
+
...constraint !== undefined ? { constraint } : {}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function isAuthValidationError(error) {
|
|
74
|
+
return error.code === "AUTH_VALIDATION_ERROR";
|
|
75
|
+
}
|
|
177
76
|
// src/domain/client.ts
|
|
178
|
-
function
|
|
77
|
+
function createValidationError(message, issues) {
|
|
179
78
|
return {
|
|
180
|
-
code: "
|
|
79
|
+
code: "ValidationError",
|
|
181
80
|
message,
|
|
182
81
|
issues
|
|
183
82
|
};
|
|
184
83
|
}
|
|
185
|
-
function
|
|
186
|
-
return error.code === "
|
|
84
|
+
function isValidationError(error) {
|
|
85
|
+
return error.code === "ValidationError";
|
|
187
86
|
}
|
|
188
|
-
function
|
|
87
|
+
function createNotFoundError(message = "Resource not found", resource) {
|
|
189
88
|
return {
|
|
190
|
-
code: "
|
|
89
|
+
code: "NotFound",
|
|
191
90
|
message,
|
|
192
91
|
resource
|
|
193
92
|
};
|
|
194
93
|
}
|
|
195
|
-
function
|
|
196
|
-
return error.code === "
|
|
94
|
+
function isNotFoundError(error) {
|
|
95
|
+
return error.code === "NotFound";
|
|
197
96
|
}
|
|
198
97
|
function createConflictError(message = "Resource conflict", field) {
|
|
199
98
|
return {
|
|
200
|
-
code: "
|
|
99
|
+
code: "Conflict",
|
|
201
100
|
message,
|
|
202
101
|
field
|
|
203
102
|
};
|
|
204
103
|
}
|
|
205
104
|
function isConflictError(error) {
|
|
206
|
-
return error.code === "
|
|
105
|
+
return error.code === "Conflict";
|
|
207
106
|
}
|
|
208
107
|
function createUnauthorizedError(message = "Authentication required") {
|
|
209
108
|
return {
|
|
210
|
-
code: "
|
|
109
|
+
code: "Unauthorized",
|
|
211
110
|
message
|
|
212
111
|
};
|
|
213
112
|
}
|
|
214
113
|
function isUnauthorizedError(error) {
|
|
215
|
-
return error.code === "
|
|
114
|
+
return error.code === "Unauthorized";
|
|
216
115
|
}
|
|
217
116
|
function createForbiddenError(message = "Access denied") {
|
|
218
117
|
return {
|
|
219
|
-
code: "
|
|
118
|
+
code: "Forbidden",
|
|
220
119
|
message
|
|
221
120
|
};
|
|
222
121
|
}
|
|
223
122
|
function isForbiddenError(error) {
|
|
224
|
-
return error.code === "
|
|
123
|
+
return error.code === "Forbidden";
|
|
225
124
|
}
|
|
226
125
|
function createRateLimitedError2(message = "Too many requests", retryAfter) {
|
|
227
126
|
return {
|
|
@@ -233,6 +132,102 @@ function createRateLimitedError2(message = "Too many requests", retryAfter) {
|
|
|
233
132
|
function isRateLimitedError2(error) {
|
|
234
133
|
return error.code === "RATE_LIMITED";
|
|
235
134
|
}
|
|
135
|
+
// src/domain/db.ts
|
|
136
|
+
function createNotFoundError2(table, key) {
|
|
137
|
+
const keyStr = key ? JSON.stringify(key) : "";
|
|
138
|
+
return {
|
|
139
|
+
code: "NotFound",
|
|
140
|
+
message: `Record not found in ${table}${keyStr ? `: ${keyStr}` : ""}`,
|
|
141
|
+
table,
|
|
142
|
+
key
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function isNotFoundError2(error) {
|
|
146
|
+
return error.code === "NotFound";
|
|
147
|
+
}
|
|
148
|
+
function createUniqueViolation(message, options) {
|
|
149
|
+
return {
|
|
150
|
+
code: "UNIQUE_VIOLATION",
|
|
151
|
+
message,
|
|
152
|
+
...options
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function isUniqueViolation(error) {
|
|
156
|
+
return error.code === "UNIQUE_VIOLATION";
|
|
157
|
+
}
|
|
158
|
+
function createFKViolation(message, options) {
|
|
159
|
+
return {
|
|
160
|
+
code: "FK_VIOLATION",
|
|
161
|
+
message,
|
|
162
|
+
...options
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function isFKViolation(error) {
|
|
166
|
+
return error.code === "FK_VIOLATION";
|
|
167
|
+
}
|
|
168
|
+
function createNotNullViolation(message, options) {
|
|
169
|
+
return {
|
|
170
|
+
code: "NOT_NULL_VIOLATION",
|
|
171
|
+
message,
|
|
172
|
+
...options
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function isNotNullViolation(error) {
|
|
176
|
+
return error.code === "NOT_NULL_VIOLATION";
|
|
177
|
+
}
|
|
178
|
+
function createCheckViolation(message, options) {
|
|
179
|
+
return {
|
|
180
|
+
code: "CHECK_VIOLATION",
|
|
181
|
+
message,
|
|
182
|
+
...options
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function isCheckViolation(error) {
|
|
186
|
+
return error.code === "CHECK_VIOLATION";
|
|
187
|
+
}
|
|
188
|
+
// src/domain/migration.ts
|
|
189
|
+
function createMigrationQueryError(message, options) {
|
|
190
|
+
return {
|
|
191
|
+
code: "MIGRATION_QUERY_ERROR",
|
|
192
|
+
message,
|
|
193
|
+
...options
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function isMigrationQueryError(error) {
|
|
197
|
+
return error.code === "MIGRATION_QUERY_ERROR";
|
|
198
|
+
}
|
|
199
|
+
function createMigrationChecksumMismatch(migrationName, expectedChecksum, actualChecksum) {
|
|
200
|
+
return {
|
|
201
|
+
code: "MIGRATION_CHECKSUM_MISMATCH",
|
|
202
|
+
message: `Migration ${migrationName} has been modified after being applied (expected: ${expectedChecksum}, actual: ${actualChecksum})`,
|
|
203
|
+
migrationName,
|
|
204
|
+
expectedChecksum,
|
|
205
|
+
actualChecksum
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function isMigrationChecksumMismatch(error) {
|
|
209
|
+
return error.code === "MIGRATION_CHECKSUM_MISMATCH";
|
|
210
|
+
}
|
|
211
|
+
function createMigrationHistoryNotFound() {
|
|
212
|
+
return {
|
|
213
|
+
code: "MIGRATION_HISTORY_NOT_FOUND",
|
|
214
|
+
message: "Migration history table does not exist. Run createHistoryTable() first."
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function isMigrationHistoryNotFound(error) {
|
|
218
|
+
return error.code === "MIGRATION_HISTORY_NOT_FOUND";
|
|
219
|
+
}
|
|
220
|
+
// src/domain/schema.ts
|
|
221
|
+
function createValidationError2(message, issues) {
|
|
222
|
+
return {
|
|
223
|
+
code: "VALIDATION_FAILED",
|
|
224
|
+
message,
|
|
225
|
+
issues
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function isValidationError2(error) {
|
|
229
|
+
return error.code === "VALIDATION_FAILED";
|
|
230
|
+
}
|
|
236
231
|
// src/infra/index.ts
|
|
237
232
|
class InfraError extends Error {
|
|
238
233
|
constructor(message) {
|
|
@@ -280,7 +275,7 @@ class SerializationError extends InfraError {
|
|
|
280
275
|
function dbErrorToHttpStatus(error) {
|
|
281
276
|
const code = error.code;
|
|
282
277
|
switch (code) {
|
|
283
|
-
case "
|
|
278
|
+
case "NotFound":
|
|
284
279
|
return 404;
|
|
285
280
|
case "UNIQUE_VIOLATION":
|
|
286
281
|
return 409;
|
|
@@ -331,7 +326,7 @@ function httpToClientError(status, body) {
|
|
|
331
326
|
case 400:
|
|
332
327
|
if (bodyObj.code === "VALIDATION_FAILED" || bodyObj.issues) {
|
|
333
328
|
const error = {
|
|
334
|
-
code: "
|
|
329
|
+
code: "ValidationError",
|
|
335
330
|
message,
|
|
336
331
|
issues: Array.isArray(bodyObj.issues) ? bodyObj.issues : undefined
|
|
337
332
|
};
|
|
@@ -340,30 +335,30 @@ function httpToClientError(status, body) {
|
|
|
340
335
|
return parseUnknownError(status, body);
|
|
341
336
|
case 401:
|
|
342
337
|
return {
|
|
343
|
-
code: "
|
|
338
|
+
code: "Unauthorized",
|
|
344
339
|
message
|
|
345
340
|
};
|
|
346
341
|
case 403:
|
|
347
342
|
return {
|
|
348
|
-
code: "
|
|
343
|
+
code: "Forbidden",
|
|
349
344
|
message
|
|
350
345
|
};
|
|
351
346
|
case 404:
|
|
352
347
|
return {
|
|
353
|
-
code: "
|
|
348
|
+
code: "NotFound",
|
|
354
349
|
message,
|
|
355
350
|
resource: typeof bodyObj.resource === "string" ? bodyObj.resource : undefined
|
|
356
351
|
};
|
|
357
352
|
case 409:
|
|
358
353
|
return {
|
|
359
|
-
code: "
|
|
354
|
+
code: "Conflict",
|
|
360
355
|
message,
|
|
361
356
|
field: typeof bodyObj.field === "string" ? bodyObj.field : undefined
|
|
362
357
|
};
|
|
363
358
|
case 422:
|
|
364
359
|
if (bodyObj.code === "VALIDATION_FAILED" || bodyObj.issues) {
|
|
365
360
|
const error = {
|
|
366
|
-
code: "
|
|
361
|
+
code: "ValidationError",
|
|
367
362
|
message,
|
|
368
363
|
issues: Array.isArray(bodyObj.issues) ? bodyObj.issues : undefined
|
|
369
364
|
};
|
|
@@ -387,6 +382,428 @@ function httpToClientError(status, body) {
|
|
|
387
382
|
function isUnknownError(error) {
|
|
388
383
|
return error.code === "UNKNOWN";
|
|
389
384
|
}
|
|
385
|
+
// src/result.ts
|
|
386
|
+
var ok = (data) => ({ ok: true, data });
|
|
387
|
+
var err = (error) => ({ ok: false, error });
|
|
388
|
+
function unwrap(result) {
|
|
389
|
+
if (result.ok) {
|
|
390
|
+
return result.data;
|
|
391
|
+
}
|
|
392
|
+
throw result.error;
|
|
393
|
+
}
|
|
394
|
+
function unwrapOr(result, defaultValue) {
|
|
395
|
+
if (result.ok) {
|
|
396
|
+
return result.data;
|
|
397
|
+
}
|
|
398
|
+
return defaultValue;
|
|
399
|
+
}
|
|
400
|
+
function map(result, fn) {
|
|
401
|
+
if (result.ok) {
|
|
402
|
+
return { ok: true, data: fn(result.data) };
|
|
403
|
+
}
|
|
404
|
+
return result;
|
|
405
|
+
}
|
|
406
|
+
function flatMap(result, fn) {
|
|
407
|
+
if (result.ok) {
|
|
408
|
+
return fn(result.data);
|
|
409
|
+
}
|
|
410
|
+
return result;
|
|
411
|
+
}
|
|
412
|
+
function match(result, handlers) {
|
|
413
|
+
return result.ok ? handlers.ok(result.data) : handlers.err(result.error);
|
|
414
|
+
}
|
|
415
|
+
function matchErr(result, handlers) {
|
|
416
|
+
if (result.ok) {
|
|
417
|
+
return handlers.ok(result.data);
|
|
418
|
+
}
|
|
419
|
+
const errorCode = result.error.code;
|
|
420
|
+
const handlersRecord = handlers;
|
|
421
|
+
const handler = handlersRecord[errorCode];
|
|
422
|
+
if (!handler) {
|
|
423
|
+
throw new Error(`Unhandled error code: ${errorCode}`);
|
|
424
|
+
}
|
|
425
|
+
return handler(result.error);
|
|
426
|
+
}
|
|
427
|
+
function isOk(result) {
|
|
428
|
+
return result.ok === true;
|
|
429
|
+
}
|
|
430
|
+
function isErr(result) {
|
|
431
|
+
return result.ok === false;
|
|
432
|
+
}
|
|
433
|
+
// src/fetch.ts
|
|
434
|
+
class FetchError extends Error {
|
|
435
|
+
code;
|
|
436
|
+
constructor(code, message) {
|
|
437
|
+
super(message);
|
|
438
|
+
this.code = code;
|
|
439
|
+
this.name = this.constructor.name;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
class FetchNetworkError extends FetchError {
|
|
444
|
+
code = "NetworkError";
|
|
445
|
+
constructor(message = "Network request failed") {
|
|
446
|
+
super("NetworkError", message);
|
|
447
|
+
this.name = "NetworkError";
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
function isFetchNetworkError(error) {
|
|
451
|
+
return error instanceof FetchNetworkError;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
class HttpError extends FetchError {
|
|
455
|
+
code = "HttpError";
|
|
456
|
+
status;
|
|
457
|
+
serverCode;
|
|
458
|
+
constructor(status, message, serverCode) {
|
|
459
|
+
super("HttpError", message);
|
|
460
|
+
this.name = "HttpError";
|
|
461
|
+
this.status = status;
|
|
462
|
+
this.serverCode = serverCode;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
function isHttpError(error) {
|
|
466
|
+
return error instanceof HttpError;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
class FetchBadRequestError extends HttpError {
|
|
470
|
+
code = "HttpError";
|
|
471
|
+
constructor(message, serverCode) {
|
|
472
|
+
super(400, message, serverCode);
|
|
473
|
+
this.name = "FetchBadRequestError";
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
function isFetchBadRequestError(error) {
|
|
477
|
+
return error instanceof FetchBadRequestError;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
class FetchUnauthorizedError extends HttpError {
|
|
481
|
+
code = "HttpError";
|
|
482
|
+
constructor(message, serverCode) {
|
|
483
|
+
super(401, message, serverCode);
|
|
484
|
+
this.name = "FetchUnauthorizedError";
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
function isFetchUnauthorizedError(error) {
|
|
488
|
+
return error instanceof FetchUnauthorizedError;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
class FetchForbiddenError extends HttpError {
|
|
492
|
+
code = "HttpError";
|
|
493
|
+
constructor(message, serverCode) {
|
|
494
|
+
super(403, message, serverCode);
|
|
495
|
+
this.name = "FetchForbiddenError";
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
function isFetchForbiddenError(error) {
|
|
499
|
+
return error instanceof FetchForbiddenError;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
class FetchNotFoundError extends HttpError {
|
|
503
|
+
code = "HttpError";
|
|
504
|
+
constructor(message, serverCode) {
|
|
505
|
+
super(404, message, serverCode);
|
|
506
|
+
this.name = "FetchNotFoundError";
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
function isFetchNotFoundError(error) {
|
|
510
|
+
return error instanceof FetchNotFoundError;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
class FetchConflictError extends HttpError {
|
|
514
|
+
code = "HttpError";
|
|
515
|
+
constructor(message, serverCode) {
|
|
516
|
+
super(409, message, serverCode);
|
|
517
|
+
this.name = "FetchConflictError";
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
function isFetchConflictError(error) {
|
|
521
|
+
return error instanceof FetchConflictError;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
class FetchGoneError extends HttpError {
|
|
525
|
+
code = "HttpError";
|
|
526
|
+
constructor(message, serverCode) {
|
|
527
|
+
super(410, message, serverCode);
|
|
528
|
+
this.name = "FetchGoneError";
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
function isFetchGoneError(error) {
|
|
532
|
+
return error instanceof FetchGoneError;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
class FetchUnprocessableEntityError extends HttpError {
|
|
536
|
+
code = "HttpError";
|
|
537
|
+
constructor(message, serverCode) {
|
|
538
|
+
super(422, message, serverCode);
|
|
539
|
+
this.name = "FetchUnprocessableEntityError";
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
function isFetchUnprocessableEntityError(error) {
|
|
543
|
+
return error instanceof FetchUnprocessableEntityError;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
class FetchRateLimitError extends HttpError {
|
|
547
|
+
code = "HttpError";
|
|
548
|
+
constructor(message, serverCode) {
|
|
549
|
+
super(429, message, serverCode);
|
|
550
|
+
this.name = "FetchRateLimitError";
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
function isFetchRateLimitError(error) {
|
|
554
|
+
return error instanceof FetchRateLimitError;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
class FetchInternalServerError extends HttpError {
|
|
558
|
+
code = "HttpError";
|
|
559
|
+
constructor(message, serverCode) {
|
|
560
|
+
super(500, message, serverCode);
|
|
561
|
+
this.name = "FetchInternalServerError";
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
function isFetchInternalServerError(error) {
|
|
565
|
+
return error instanceof FetchInternalServerError;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
class FetchServiceUnavailableError extends HttpError {
|
|
569
|
+
code = "HttpError";
|
|
570
|
+
constructor(message, serverCode) {
|
|
571
|
+
super(503, message, serverCode);
|
|
572
|
+
this.name = "FetchServiceUnavailableError";
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
function isFetchServiceUnavailableError(error) {
|
|
576
|
+
return error instanceof FetchServiceUnavailableError;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
class FetchTimeoutError extends FetchError {
|
|
580
|
+
code = "TimeoutError";
|
|
581
|
+
constructor(message = "Request timed out") {
|
|
582
|
+
super("TimeoutError", message);
|
|
583
|
+
this.name = "TimeoutError";
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
function isFetchTimeoutError(error) {
|
|
587
|
+
return error instanceof FetchTimeoutError;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
class ParseError extends FetchError {
|
|
591
|
+
code = "ParseError";
|
|
592
|
+
path;
|
|
593
|
+
value;
|
|
594
|
+
constructor(path, message, value) {
|
|
595
|
+
super("ParseError", message);
|
|
596
|
+
this.name = "ParseError";
|
|
597
|
+
this.path = path;
|
|
598
|
+
this.value = value;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
function isParseError(error) {
|
|
602
|
+
return error instanceof ParseError;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
class FetchValidationError extends FetchError {
|
|
606
|
+
code = "ValidationError";
|
|
607
|
+
errors;
|
|
608
|
+
constructor(message, errors) {
|
|
609
|
+
super("ValidationError", message);
|
|
610
|
+
this.name = "ValidationError";
|
|
611
|
+
this.errors = errors;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
function isFetchValidationError(error) {
|
|
615
|
+
return error instanceof FetchValidationError;
|
|
616
|
+
}
|
|
617
|
+
function createHttpError(status, message, serverCode) {
|
|
618
|
+
switch (status) {
|
|
619
|
+
case 400:
|
|
620
|
+
return new FetchBadRequestError(message, serverCode);
|
|
621
|
+
case 401:
|
|
622
|
+
return new FetchUnauthorizedError(message, serverCode);
|
|
623
|
+
case 403:
|
|
624
|
+
return new FetchForbiddenError(message, serverCode);
|
|
625
|
+
case 404:
|
|
626
|
+
return new FetchNotFoundError(message, serverCode);
|
|
627
|
+
case 409:
|
|
628
|
+
return new FetchConflictError(message, serverCode);
|
|
629
|
+
case 410:
|
|
630
|
+
return new FetchGoneError(message, serverCode);
|
|
631
|
+
case 422:
|
|
632
|
+
return new FetchUnprocessableEntityError(message, serverCode);
|
|
633
|
+
case 429:
|
|
634
|
+
return new FetchRateLimitError(message, serverCode);
|
|
635
|
+
case 500:
|
|
636
|
+
return new FetchInternalServerError(message, serverCode);
|
|
637
|
+
case 503:
|
|
638
|
+
return new FetchServiceUnavailableError(message, serverCode);
|
|
639
|
+
default:
|
|
640
|
+
return new HttpError(status, message, serverCode);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
// src/entity.ts
|
|
644
|
+
class EntityError extends Error {
|
|
645
|
+
code;
|
|
646
|
+
constructor(code, message) {
|
|
647
|
+
super(message);
|
|
648
|
+
this.code = code;
|
|
649
|
+
this.name = this.constructor.name;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
class BadRequestError extends EntityError {
|
|
654
|
+
code = "BadRequest";
|
|
655
|
+
constructor(message = "Bad Request") {
|
|
656
|
+
super("BadRequest", message);
|
|
657
|
+
this.name = "BadRequestError";
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
function isBadRequestError(error) {
|
|
661
|
+
return error instanceof BadRequestError;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
class EntityUnauthorizedError extends EntityError {
|
|
665
|
+
code = "Unauthorized";
|
|
666
|
+
constructor(message = "Unauthorized") {
|
|
667
|
+
super("Unauthorized", message);
|
|
668
|
+
this.name = "UnauthorizedError";
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
function isEntityUnauthorizedError(error) {
|
|
672
|
+
return error instanceof EntityUnauthorizedError;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
class EntityForbiddenError extends EntityError {
|
|
676
|
+
code = "Forbidden";
|
|
677
|
+
constructor(message = "Forbidden") {
|
|
678
|
+
super("Forbidden", message);
|
|
679
|
+
this.name = "ForbiddenError";
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
function isEntityForbiddenError(error) {
|
|
683
|
+
return error instanceof EntityForbiddenError;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
class EntityNotFoundError extends EntityError {
|
|
687
|
+
code = "NotFound";
|
|
688
|
+
resource;
|
|
689
|
+
resourceId;
|
|
690
|
+
constructor(message = "Not Found", resource, resourceId) {
|
|
691
|
+
super("NotFound", message);
|
|
692
|
+
this.name = "NotFoundError";
|
|
693
|
+
this.resource = resource;
|
|
694
|
+
this.resourceId = resourceId;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
function isEntityNotFoundError(error) {
|
|
698
|
+
return error instanceof EntityNotFoundError;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
class MethodNotAllowedError extends EntityError {
|
|
702
|
+
code = "MethodNotAllowed";
|
|
703
|
+
allowedMethods;
|
|
704
|
+
constructor(allowedMethods, message = "Method Not Allowed") {
|
|
705
|
+
super("MethodNotAllowed", message);
|
|
706
|
+
this.name = "MethodNotAllowedError";
|
|
707
|
+
this.allowedMethods = allowedMethods;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
function isMethodNotAllowedError(error) {
|
|
711
|
+
return error instanceof MethodNotAllowedError;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
class EntityConflictError extends EntityError {
|
|
715
|
+
code = "Conflict";
|
|
716
|
+
field;
|
|
717
|
+
constructor(message = "Conflict", field) {
|
|
718
|
+
super("Conflict", message);
|
|
719
|
+
this.name = "ConflictError";
|
|
720
|
+
this.field = field;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
function isEntityConflictError(error) {
|
|
724
|
+
return error instanceof EntityConflictError;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
class EntityValidationError extends EntityError {
|
|
728
|
+
code = "ValidationError";
|
|
729
|
+
errors;
|
|
730
|
+
constructor(errors) {
|
|
731
|
+
super("ValidationError", "Validation failed");
|
|
732
|
+
this.name = "EntityValidationError";
|
|
733
|
+
this.errors = errors;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
function isEntityValidationError(error) {
|
|
737
|
+
return error instanceof EntityValidationError;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
class InternalError extends EntityError {
|
|
741
|
+
code = "InternalError";
|
|
742
|
+
constructor(message = "Internal Server Error") {
|
|
743
|
+
super("InternalError", message);
|
|
744
|
+
this.name = "InternalError";
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
function isInternalError(error) {
|
|
748
|
+
return error instanceof InternalError;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
class ServiceUnavailableError extends EntityError {
|
|
752
|
+
code = "ServiceUnavailable";
|
|
753
|
+
retryAfter;
|
|
754
|
+
constructor(message = "Service Unavailable", retryAfter) {
|
|
755
|
+
super("ServiceUnavailable", message);
|
|
756
|
+
this.name = "ServiceUnavailableError";
|
|
757
|
+
this.retryAfter = retryAfter;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
function isServiceUnavailableError(error) {
|
|
761
|
+
return error instanceof ServiceUnavailableError;
|
|
762
|
+
}
|
|
763
|
+
// src/match-error.ts
|
|
764
|
+
function matchError(error, handlers) {
|
|
765
|
+
const isFetchError = error.code === "NetworkError" || error.code === "HttpError" || error.code === "TimeoutError" || error.code === "ParseError" || error.code === "ValidationError" && error.name !== "EntityValidationError";
|
|
766
|
+
if (isFetchError) {
|
|
767
|
+
const code2 = error.code;
|
|
768
|
+
switch (code2) {
|
|
769
|
+
case "NetworkError":
|
|
770
|
+
return handlers.NetworkError(error);
|
|
771
|
+
case "HttpError":
|
|
772
|
+
return handlers.HttpError(error);
|
|
773
|
+
case "TimeoutError":
|
|
774
|
+
return handlers.TimeoutError(error);
|
|
775
|
+
case "ParseError":
|
|
776
|
+
return handlers.ParseError(error);
|
|
777
|
+
case "ValidationError":
|
|
778
|
+
return handlers.ValidationError(error);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
const code = error.code;
|
|
782
|
+
switch (code) {
|
|
783
|
+
case "BadRequest":
|
|
784
|
+
return handlers.BadRequest(error);
|
|
785
|
+
case "Unauthorized":
|
|
786
|
+
return handlers.Unauthorized(error);
|
|
787
|
+
case "Forbidden":
|
|
788
|
+
return handlers.Forbidden(error);
|
|
789
|
+
case "NotFound":
|
|
790
|
+
return handlers.NotFound(error);
|
|
791
|
+
case "MethodNotAllowed":
|
|
792
|
+
return handlers.MethodNotAllowed(error);
|
|
793
|
+
case "Conflict":
|
|
794
|
+
return handlers.Conflict(error);
|
|
795
|
+
case "ValidationError":
|
|
796
|
+
return handlers.ValidationError(error);
|
|
797
|
+
case "InternalError":
|
|
798
|
+
return handlers.InternalError(error);
|
|
799
|
+
case "ServiceUnavailable":
|
|
800
|
+
return handlers.ServiceUnavailable(error);
|
|
801
|
+
}
|
|
802
|
+
const checkExhaustive = (c) => {
|
|
803
|
+
throw new Error(`Unhandled error code: ${c}`);
|
|
804
|
+
};
|
|
805
|
+
return checkExhaustive(code);
|
|
806
|
+
}
|
|
390
807
|
export {
|
|
391
808
|
unwrapOr,
|
|
392
809
|
unwrap,
|
|
@@ -394,6 +811,7 @@ export {
|
|
|
394
811
|
ok,
|
|
395
812
|
notNullViolationToHttpStatus,
|
|
396
813
|
notFoundErrorToHttpStatus,
|
|
814
|
+
matchError,
|
|
397
815
|
matchErr,
|
|
398
816
|
match,
|
|
399
817
|
map,
|
|
@@ -402,20 +820,48 @@ export {
|
|
|
402
820
|
isUniqueViolation,
|
|
403
821
|
isUnauthorizedError,
|
|
404
822
|
isSessionExpiredError,
|
|
405
|
-
|
|
823
|
+
isServiceUnavailableError,
|
|
824
|
+
isValidationError2 as isSchemaValidationError,
|
|
406
825
|
isPermissionDeniedError,
|
|
826
|
+
isParseError,
|
|
407
827
|
isOk,
|
|
408
828
|
isNotNullViolation,
|
|
829
|
+
isMigrationQueryError,
|
|
830
|
+
isMigrationHistoryNotFound,
|
|
831
|
+
isMigrationChecksumMismatch,
|
|
832
|
+
isMethodNotAllowedError,
|
|
409
833
|
isInvalidCredentialsError,
|
|
834
|
+
isInternalError,
|
|
835
|
+
isHttpError,
|
|
410
836
|
isForbiddenError,
|
|
837
|
+
isFetchValidationError,
|
|
838
|
+
isFetchUnprocessableEntityError,
|
|
839
|
+
isFetchUnauthorizedError,
|
|
840
|
+
isFetchTimeoutError,
|
|
841
|
+
isFetchServiceUnavailableError,
|
|
842
|
+
isFetchRateLimitError,
|
|
843
|
+
isFetchNotFoundError,
|
|
844
|
+
isFetchNetworkError,
|
|
845
|
+
isFetchInternalServerError,
|
|
846
|
+
isFetchGoneError,
|
|
847
|
+
isFetchForbiddenError,
|
|
848
|
+
isFetchConflictError,
|
|
849
|
+
isFetchBadRequestError,
|
|
411
850
|
isFKViolation,
|
|
412
851
|
isErr,
|
|
413
|
-
|
|
852
|
+
isEntityValidationError,
|
|
853
|
+
isEntityUnauthorizedError,
|
|
854
|
+
isEntityNotFoundError,
|
|
855
|
+
isEntityForbiddenError,
|
|
856
|
+
isEntityConflictError,
|
|
857
|
+
isNotFoundError2 as isDBNotFoundError,
|
|
414
858
|
isConflictError,
|
|
415
|
-
|
|
859
|
+
isValidationError as isClientValidationError,
|
|
416
860
|
isRateLimitedError2 as isClientRateLimitedError,
|
|
417
|
-
|
|
861
|
+
isNotFoundError as isClientNotFoundError,
|
|
418
862
|
isCheckViolation,
|
|
863
|
+
isBadRequestError,
|
|
864
|
+
isAuthValidationError,
|
|
419
865
|
isRateLimitedError as isAuthRateLimitedError,
|
|
420
866
|
httpToClientError,
|
|
421
867
|
flatMap,
|
|
@@ -426,26 +872,57 @@ export {
|
|
|
426
872
|
createUniqueViolation,
|
|
427
873
|
createUnauthorizedError,
|
|
428
874
|
createSessionExpiredError,
|
|
429
|
-
|
|
875
|
+
createValidationError2 as createSchemaValidationError,
|
|
430
876
|
createPermissionDeniedError,
|
|
431
877
|
createNotNullViolation,
|
|
878
|
+
createMigrationQueryError,
|
|
879
|
+
createMigrationHistoryNotFound,
|
|
880
|
+
createMigrationChecksumMismatch,
|
|
432
881
|
createInvalidCredentialsError,
|
|
882
|
+
createHttpError,
|
|
433
883
|
createForbiddenError,
|
|
434
884
|
createFKViolation,
|
|
435
|
-
|
|
885
|
+
createNotFoundError2 as createDBNotFoundError,
|
|
436
886
|
createConflictError,
|
|
437
|
-
|
|
887
|
+
createValidationError as createClientValidationError,
|
|
438
888
|
createRateLimitedError2 as createClientRateLimitedError,
|
|
439
|
-
|
|
889
|
+
createNotFoundError as createClientNotFoundError,
|
|
440
890
|
createCheckViolation,
|
|
891
|
+
createAuthValidationError,
|
|
441
892
|
createRateLimitedError as createAuthRateLimitedError,
|
|
442
893
|
checkViolationToHttpStatus,
|
|
443
894
|
TimeoutError,
|
|
895
|
+
ServiceUnavailableError,
|
|
444
896
|
SerializationError,
|
|
445
897
|
QueryError,
|
|
446
898
|
PoolExhaustedError,
|
|
899
|
+
ParseError,
|
|
447
900
|
NetworkError,
|
|
901
|
+
MethodNotAllowedError,
|
|
902
|
+
InternalError,
|
|
448
903
|
InfraError,
|
|
904
|
+
HttpError,
|
|
905
|
+
FetchValidationError,
|
|
906
|
+
FetchUnprocessableEntityError,
|
|
907
|
+
FetchUnauthorizedError,
|
|
908
|
+
FetchTimeoutError,
|
|
909
|
+
FetchServiceUnavailableError,
|
|
910
|
+
FetchRateLimitError,
|
|
911
|
+
FetchNotFoundError,
|
|
912
|
+
FetchNetworkError,
|
|
913
|
+
FetchInternalServerError,
|
|
914
|
+
FetchGoneError,
|
|
915
|
+
FetchForbiddenError,
|
|
916
|
+
FetchError,
|
|
917
|
+
FetchConflictError,
|
|
918
|
+
FetchBadRequestError,
|
|
919
|
+
EntityValidationError,
|
|
920
|
+
EntityUnauthorizedError,
|
|
921
|
+
EntityNotFoundError,
|
|
922
|
+
EntityForbiddenError,
|
|
923
|
+
EntityError,
|
|
924
|
+
EntityConflictError,
|
|
449
925
|
ConnectionError,
|
|
926
|
+
BadRequestError,
|
|
450
927
|
AppError
|
|
451
928
|
};
|