25 lines · 668 B
| 1 | import { AppError } from '@gitfastr/shared/utils/errors.js'; |
| 2 | |
| 3 | /** Global error handler: converts errors to JSON responses. */ |
| 4 | export function handleError(error: unknown): Response { |
| 5 | if (error instanceof AppError) { |
| 6 | return new Response( |
| 7 | JSON.stringify({ error: error.message, code: error.code }), |
| 8 | { |
| 9 | status: error.statusCode, |
| 10 | headers: { 'Content-Type': 'application/json' }, |
| 11 | } |
| 12 | ); |
| 13 | } |
| 14 | |
| 15 | console.error('Unhandled error:', error); |
| 16 | |
| 17 | return new Response( |
| 18 | JSON.stringify({ error: 'Internal server error', code: 'INTERNAL_ERROR' }), |
| 19 | { |
| 20 | status: 500, |
| 21 | headers: { 'Content-Type': 'application/json' }, |
| 22 | } |
| 23 | ); |
| 24 | } |
| 25 |