|
1 | 1 | import type { ErrorCode } from '~/errors/error-codes';
|
2 | 2 | import { ErrorCodes } from '~/errors/error-codes';
|
| 3 | +import type { HttpStatusCode } from '~/errors/http-status-codes'; |
3 | 4 | import { randomString } from '~/utils/string-utils';
|
4 | 5 |
|
5 |
| -// prettier-ignore |
6 |
| -export type HttpStatusCode = |
7 |
| - | 100 | 101 | 102 | 103 |
8 |
| - | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226 |
9 |
| - | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 |
10 |
| - | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 |
11 |
| - | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511; |
12 |
| - |
13 |
| -export type CodedErrorOpts = { |
| 6 | +export type ApplicationErrorOptions = { |
14 | 7 | correlationId?: string;
|
15 |
| - statusCode?: HttpStatusCode; |
| 8 | + httpStatusCode?: HttpStatusCode; |
16 | 9 | };
|
17 | 10 |
|
18 | 11 | /**
|
19 | 12 | * A generic, top-level error that all application errors should extend.
|
20 | 13 | * This class *does not* extend Error because React Router will sanitize all Errors when sending them to the client.
|
21 | 14 | */
|
22 |
| -export class CodedError { |
23 |
| - public readonly name = 'CodedError'; |
| 15 | +export class ApplicationError { |
| 16 | + public readonly name = 'ApplicationError'; |
24 | 17 |
|
25 | 18 | public readonly errorCode: ErrorCode;
|
26 | 19 | public readonly correlationId: string;
|
27 | 20 | public readonly stack?: string;
|
28 |
| - public readonly statusCode: HttpStatusCode; |
| 21 | + public readonly httpStatusCode: HttpStatusCode; |
29 | 22 |
|
30 | 23 | // note: this is intentionally named `msg` instead
|
31 | 24 | // of `message` to workaround an issue with winston
|
32 | 25 | // always logging this as the log message when a
|
33 | 26 | // message is supplied to `log.error(message, error)`
|
34 | 27 | public readonly msg: string;
|
35 | 28 |
|
36 |
| - public constructor(msg: string, errorCode: ErrorCode = ErrorCodes.UNCAUGHT_ERROR, opts?: CodedErrorOpts) { |
| 29 | + public constructor(msg: string, errorCode: ErrorCode = ErrorCodes.UNCAUGHT_ERROR, opts?: ApplicationErrorOptions) { |
37 | 30 | this.errorCode = errorCode;
|
38 | 31 | this.msg = msg;
|
39 | 32 |
|
40 | 33 | this.correlationId = opts?.correlationId ?? generateCorrelationId();
|
41 |
| - this.statusCode = opts?.statusCode ?? 500; |
| 34 | + this.httpStatusCode = opts?.httpStatusCode ?? 500; |
42 | 35 |
|
43 | 36 | Error.captureStackTrace(this, this.constructor);
|
44 | 37 | }
|
45 | 38 | }
|
46 | 39 |
|
47 | 40 | /**
|
48 |
| - * Type guard to check if an error is a CodedError. |
| 41 | + * Type guard to check if an error is a ApplicationError. |
49 | 42 | *
|
50 | 43 | * Note: this function does not use `instanceof` because the type
|
51 | 44 | * information is lost when shipped to the client
|
52 | 45 | */
|
53 |
| -export function isCodedError(error: unknown): error is CodedError { |
54 |
| - return error instanceof Object && 'name' in error && error.name === 'CodedError'; |
| 46 | +export function isApplicationError(error: unknown): error is ApplicationError { |
| 47 | + return error instanceof Object && 'name' in error && error.name === 'ApplicationError'; |
55 | 48 | }
|
56 | 49 |
|
57 | 50 | /**
|
|
0 commit comments