| 67 | |
| 68 | /** Send a failure. Anything that is not an {@link ApiError} becomes a 500. */ |
| 69 | export function fail(res: ServerResponse, err: unknown, method: string): true { |
| 70 | if (err instanceof ApiError) { |
| 71 | const body: { error: string; code: ApiErrorCode; hint?: string } = { |
| 72 | error: err.message, |
| 73 | code: err.code, |
| 74 | }; |
| 75 | if (err.hint) body.hint = err.hint; |
| 76 | sendJson(res, STATUS[err.code], body, method); |
| 77 | return true; |
| 78 | } |
| 79 | sendJson( |
| 80 | res, |
| 81 | 500, |
| 82 | { |
| 83 | error: err instanceof Error ? err.message : String(err), |
| 84 | code: 'internal' satisfies ApiErrorCode, |
| 85 | }, |
| 86 | method |
| 87 | ); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | // ============================================================================= |
| 92 | // Request bodies |