| 13 | } |
| 14 | |
| 15 | export async function forwardError(c: Context, error: unknown) { |
| 16 | consola.error("Error occurred:", error) |
| 17 | |
| 18 | if (error instanceof HTTPError) { |
| 19 | const errorText = await error.response.text() |
| 20 | let errorJson: unknown |
| 21 | try { |
| 22 | errorJson = JSON.parse(errorText) |
| 23 | } catch { |
| 24 | errorJson = errorText |
| 25 | } |
| 26 | consola.error("HTTP error:", errorJson) |
| 27 | return c.json( |
| 28 | { |
| 29 | error: { |
| 30 | message: errorText, |
| 31 | type: "error", |
| 32 | }, |
| 33 | }, |
| 34 | error.response.status as ContentfulStatusCode, |
| 35 | ) |
| 36 | } |
| 37 | |
| 38 | return c.json( |
| 39 | { |
| 40 | error: { |
| 41 | message: (error as Error).message, |
| 42 | type: "error", |
| 43 | }, |
| 44 | }, |
| 45 | 500, |
| 46 | ) |
| 47 | } |