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
47 lines (41 loc) · 985 Bytes
/
error.ts
File metadata and controls
47 lines (41 loc) · 985 Bytes
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
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) {
consola.error("Error occurred:", error)
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:", errorJson)
return c.json(
{
error: {
message: errorText,
type: "error",
},
},
error.response.status as ContentfulStatusCode,
)
}
return c.json(
{
error: {
message: (error as Error).message,
type: "error",
},
},
500,
)
}