copilot-api / src /lib /error.ts
imseldrith's picture
Initial upload from Colab
9e27976 verified
raw
history blame contribute delete
985 Bytes
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,
)
}