The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics-next-line extendsNativeError:off - vendored upstream code without Effect; kept as published. |
| 2 | export class HttpError extends Error { |
| 3 | constructor( |
| 4 | public status: number, |
| 5 | public code: string, |
| 6 | ) { |
| 7 | super(code); |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | export function badRequest(): never { |
| 12 | throw new HttpError(400, 'invalid_request'); |
| 13 | } |
| 14 | |
| 15 | export function tooLarge(): never { |
| 16 | throw new HttpError(413, 'size_limit'); |
| 17 | } |
| 18 | |
| 19 | export function unavailable(): never { |
| 20 | throw new HttpError(503, 'unavailable'); |
| 21 | } |
| 22 | |
| 23 | export function errorResponse(error: unknown): Response { |
| 24 | const status = error instanceof HttpError ? error.status : 500; |
| 25 | const messages: Record<number, string> = { |
| 26 | 400: 'Invalid request', |
| 27 | 401: 'Invalid credentials', |
| 28 | 403: 'Write not permitted', |
| 29 | 404: 'Not found', |
| 30 | 413: 'Request too large', |
| 31 | 429: 'Rate limit exceeded', |
| 32 | 500: 'Operation failed', |
| 33 | 503: 'Service unavailable', |
| 34 | }; |
| 35 | return new Response(messages[status] ?? messages[500], { |
| 36 | status, |
| 37 | headers: { |
| 38 | 'Content-Type': 'text/plain; charset=utf-8', |
| 39 | 'Cache-Control': 'no-store', |
| 40 | ...(status === 429 || status === 503 ? { 'Retry-After': '60' } : {}), |
| 41 | }, |
| 42 | }); |
| 43 | } |