|
1 |
| -import type { JsonError } from './types' |
2 |
| - |
3 |
| -export const error = < |
4 |
| - TProperties extends { status?: number }, |
5 |
| - TError = TProperties & JsonError |
6 |
| ->( |
7 |
| - properties: TProperties |
8 |
| -): TError => |
9 |
| - ({ |
10 |
| - format: '@json', |
11 |
| - ...properties, |
12 |
| - status: properties.status ?? 500 |
13 |
| - } as TError) |
| 1 | +type Json = string | number | boolean | { [x: string]: Json } | Array<Json> |
| 2 | + |
| 3 | +export const isError = (err: any): err is ApiError => { |
| 4 | + return err instanceof ApiError |
| 5 | +} |
| 6 | + |
| 7 | +export type ErrorProperties = { message: string; status: number } & Record< |
| 8 | + string, |
| 9 | + Json |
| 10 | +> |
| 11 | + |
| 12 | +export type ErrorPropertiesWithoutStatus = { |
| 13 | + message?: string |
| 14 | + status?: never |
| 15 | +} & Record<string, Json> |
| 16 | + |
| 17 | +/** |
| 18 | + * This error class is designed to be returned to the |
| 19 | + * user. When thrown, eventually a root hook will |
| 20 | + * handle it, convert it to json, and return it in a |
| 21 | + * response. |
| 22 | + */ |
| 23 | +export class ApiError extends Error { |
| 24 | + status: number |
| 25 | + properties: ErrorProperties |
| 26 | + // readonly _key: string = '@exo.error' |
| 27 | + constructor( |
| 28 | + /** |
| 29 | + * Any json serializable value is allowed in |
| 30 | + * the object, the entire object will be |
| 31 | + * serialized and returned to the user. |
| 32 | + */ |
| 33 | + error: ErrorProperties |
| 34 | + ) { |
| 35 | + super(error.message) |
| 36 | + // Set the prototype explicitly so that instanceof |
| 37 | + // will work as expeted. Object.setPrototypeOf needs |
| 38 | + // to be called immediately after the super(...) call |
| 39 | + // https://stackoverflow.com/a/41429145/7547940 |
| 40 | + Object.setPrototypeOf(this, ApiError.prototype) |
| 41 | + this.status = error.status ?? 500 |
| 42 | + this.properties = error |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// |
| 47 | +// Just the few most commonly used |
| 48 | +// errors for convenience. |
| 49 | +// |
| 50 | + |
| 51 | +export class BadRequestError extends ApiError { |
| 52 | + constructor(error: ErrorPropertiesWithoutStatus) { |
| 53 | + super({ |
| 54 | + ...error, |
| 55 | + status: 400, |
| 56 | + message: error.message ?? 'Bad Request' |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export class NotAuthenticatedError extends ApiError { |
| 62 | + constructor(error: ErrorPropertiesWithoutStatus) { |
| 63 | + super({ |
| 64 | + ...error, |
| 65 | + status: 401, |
| 66 | + message: error.message ?? 'Not Authenticated' |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export class NotAuthorizedError extends ApiError { |
| 72 | + constructor(error: ErrorPropertiesWithoutStatus) { |
| 73 | + super({ |
| 74 | + ...error, |
| 75 | + status: 403, |
| 76 | + message: error.message ?? 'Not Authorized' |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export class NotFoundError extends ApiError { |
| 82 | + constructor(error: ErrorPropertiesWithoutStatus) { |
| 83 | + super({ |
| 84 | + ...error, |
| 85 | + status: 404, |
| 86 | + message: error.message ?? 'Not Found' |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export class MethodNotAllowedError extends ApiError { |
| 92 | + constructor(error: ErrorPropertiesWithoutStatus) { |
| 93 | + super({ |
| 94 | + ...error, |
| 95 | + status: 405, |
| 96 | + message: error.message ?? 'Method Not Allowed' |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export class TooManyRequestsError extends ApiError { |
| 102 | + constructor(error: ErrorPropertiesWithoutStatus) { |
| 103 | + super({ |
| 104 | + ...error, |
| 105 | + status: 429, |
| 106 | + message: error.message ?? 'Too Many Requests' |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments