Skip to content

Commit 88b196a

Browse files
committed
feat: Implement centralized error handling for chat completions route
1 parent 50df7fc commit 88b196a

1 file changed

Lines changed: 88 additions & 13 deletions

File tree

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import type { BlankEnv, BlankInput } from "hono/types"
2+
import type { ContentfulStatusCode } from "hono/utils/http-status"
3+
14
import consola from "consola"
2-
import { Hono } from "hono"
5+
import { Hono, type Context } from "hono"
36
import { FetchError } from "ofetch"
47

58
import { handlerStreaming } from "./handler"
@@ -10,17 +13,89 @@ completionRoutes.post("/", async (c) => {
1013
try {
1114
return await handlerStreaming(c)
1215
} catch (error) {
13-
if (error instanceof FetchError) {
14-
consola.error(`Request failed: ${error.message}`, error.response?._data)
15-
}
16-
if (error instanceof Response) {
17-
consola.error(
18-
`Request failed: ${error.status} ${error.statusText}: ${await error.text()}`,
19-
)
20-
} else if (error instanceof Error) {
21-
consola.error("Error:", error.message)
22-
} else {
23-
consola.error("Error:", error)
24-
}
16+
consola.error("Error occurred:", error)
17+
return handleError(c, error)
2518
}
2619
})
20+
21+
// Handle different error types with specific handlers
22+
async function handleError(
23+
c: Context<BlankEnv, "/", BlankInput>,
24+
error: unknown,
25+
) {
26+
if (error instanceof FetchError) {
27+
return handleFetchError(c, error)
28+
}
29+
30+
if (error instanceof Response) {
31+
return await handleResponseError(c, error)
32+
}
33+
34+
if (error instanceof Error) {
35+
return handleGenericError(c, error)
36+
}
37+
38+
// Fallback for unknown error types
39+
return c.json(
40+
{
41+
error: {
42+
message: "An unknown error occurred",
43+
type: "unknown_error",
44+
},
45+
},
46+
500,
47+
)
48+
}
49+
50+
function handleFetchError(
51+
c: Context<BlankEnv, "/", BlankInput>,
52+
error: FetchError,
53+
) {
54+
return c.json(
55+
{
56+
error: {
57+
message: error.message,
58+
type: "fetch_error",
59+
data: error.response?._data as unknown,
60+
},
61+
},
62+
(error.response?.status ?? 500) as ContentfulStatusCode,
63+
)
64+
}
65+
66+
async function handleResponseError(
67+
c: Context<BlankEnv, "/", BlankInput>,
68+
error: Response,
69+
) {
70+
const errorText = await error.text()
71+
consola.error(
72+
`Request failed: ${error.status} ${error.statusText}: ${errorText}`,
73+
)
74+
75+
return c.json(
76+
{
77+
error: {
78+
message: error.statusText || "Request failed",
79+
type: "response_error",
80+
status: error.status,
81+
details: errorText,
82+
},
83+
},
84+
error.status as ContentfulStatusCode,
85+
)
86+
}
87+
88+
function handleGenericError(
89+
c: Context<BlankEnv, "/", BlankInput>,
90+
error: Error,
91+
) {
92+
return c.json(
93+
{
94+
error: {
95+
message: error.message,
96+
type: "error",
97+
},
98+
},
99+
500,
100+
)
101+
}

0 commit comments

Comments
 (0)