1- import type { BlankEnv , BlankInput } from "hono/types"
2- import type { ContentfulStatusCode } from "hono/utils/http-status"
3-
41import consola from "consola"
5- import { Hono , type Context } from "hono"
6- import { FetchError } from "ofetch"
2+ import { Hono } from "hono"
73
8- import { logger } from "../../lib/logger"
94import { handleCompletion } from "./handler"
105
116export const completionRoutes = new Hono ( )
@@ -15,149 +10,27 @@ completionRoutes.post("/", async (c) => {
1510 return await handleCompletion ( c )
1611 } catch ( error ) {
1712 consola . error ( "Error occurred:" , error )
18- return handleError ( c , error )
19- }
20- } )
21-
22- // Handle different error types with specific handlers
23- async function handleError (
24- c : Context < BlankEnv , "/" , BlankInput > ,
25- error : unknown ,
26- ) {
27- if ( error instanceof FetchError ) {
28- return handleFetchError ( c , error )
29- }
30-
31- if ( error instanceof Response ) {
32- return await handleResponseError ( c , error )
33- }
34-
35- if ( error instanceof Error ) {
36- return handleGenericError ( c , error )
37- }
38-
39- // Fallback for unknown error types
40- void logger . logResponse ( "/v1/chat/completions" , {
41- error : {
42- message : "An unknown error occurred" ,
43- type : "unknown_error" ,
44- } ,
45- } )
46-
47- return c . json (
48- {
49- error : {
50- message : "An unknown error occurred" ,
51- type : "unknown_error" ,
52- } ,
53- } ,
54- 500 ,
55- )
56- }
57-
58- function handleFetchError (
59- c : Context < BlankEnv , "/" , BlankInput > ,
60- error : FetchError ,
61- ) {
62- const status = error . response ?. status ?? 500
63- const responseData = error . response ?. _data as unknown
64- const headers : Record < string , string > = { }
65-
66- // Forward all headers from the error response
67- for ( const [ key , value ] of error . response ?. headers . entries ( ) ) {
68- c . header ( key , value )
69- headers [ key ] = value
70- }
71-
72- // Log the error response
73- void logger . logResponse (
74- "/v1/chat/completions" ,
75- {
76- error : {
77- message : error . message ,
78- type : "fetch_error" ,
79- data : responseData ,
80- status,
81- } ,
82- } ,
83- headers ,
84- )
8513
86- return c . json (
87- {
88- error : {
89- message : error . message ,
90- type : "fetch_error" ,
91- data : responseData ,
14+ if ( error instanceof Response ) {
15+ return c . json (
16+ {
17+ error : {
18+ message : error . message ,
19+ type : "error" ,
20+ } ,
21+ } ,
22+ 500 ,
23+ )
24+ }
25+
26+ return c . json (
27+ {
28+ error : {
29+ message : error . message ,
30+ type : "error" ,
31+ } ,
9232 } ,
93- } ,
94- status as ContentfulStatusCode ,
95- )
96- }
97-
98- async function handleResponseError (
99- c : Context < BlankEnv , "/" , BlankInput > ,
100- error : Response ,
101- ) {
102- const errorText = await error . text ( )
103- consola . error (
104- `Request failed: ${ error . status } ${ error . statusText } : ${ errorText } ` ,
105- )
106-
107- const headers : Record < string , string > = { }
108-
109- // Forward all headers from the error response
110- for ( const [ key , value ] of error . headers . entries ( ) ) {
111- c . header ( key , value )
112- headers [ key ] = value
33+ 500 ,
34+ )
11335 }
114-
115- // Log the error response
116- void logger . logResponse (
117- "/v1/chat/completions" ,
118- {
119- error : {
120- message : error . statusText || "Request failed" ,
121- type : "response_error" ,
122- status : error . status ,
123- details : errorText ,
124- } ,
125- } ,
126- headers ,
127- )
128-
129- return c . json (
130- {
131- error : {
132- message : error . statusText || "Request failed" ,
133- type : "response_error" ,
134- status : error . status ,
135- details : errorText ,
136- } ,
137- } ,
138- error . status as ContentfulStatusCode ,
139- )
140- }
141-
142- function handleGenericError (
143- c : Context < BlankEnv , "/" , BlankInput > ,
144- error : Error ,
145- ) {
146- // Log the error response
147- void logger . logResponse ( "/v1/chat/completions" , {
148- error : {
149- message : error . message ,
150- type : "error" ,
151- } ,
152- } )
153-
154- return c . json (
155- {
156- error : {
157- message : error . message ,
158- type : "error" ,
159- } ,
160- } ,
161- 500 ,
162- )
163- }
36+ } )
0 commit comments