forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.ts
More file actions
50 lines (45 loc) · 1.13 KB
/
error.ts
File metadata and controls
50 lines (45 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import type { Context } from "hono"
import type { ContentfulStatusCode } from "hono/utils/http-status"
import consola from "consola"
export class HTTPError extends Error {
response: Response
constructor(message: string, response: Response) {
super(message)
this.response = response
}
}
export async function forwardError(c: Context, error: unknown) {
if (error instanceof HTTPError) {
const errorText = await error.response.text()
let errorJson: unknown
try {
errorJson = JSON.parse(errorText)
} catch {
errorJson = errorText
}
consola.error(
`HTTP ${error.response.status} error from ${error.response.url}:`,
errorJson,
)
return c.json(
{
error: {
message: errorText,
type: "error",
},
},
error.response.status as ContentfulStatusCode,
)
}
const err = error instanceof Error ? error : new Error(String(error))
consola.error(`Unhandled error in request: ${err.message}`, err.stack ?? err)
return c.json(
{
error: {
message: err.message,
type: "error",
},
},
500,
)
}