@@ -5,6 +5,7 @@ import consola from "consola"
55import { Hono , type Context } from "hono"
66import { FetchError } from "ofetch"
77
8+ import { logger } from "../../lib/logger"
89import { handlerStreaming } from "./handler"
910
1011export const completionRoutes = new Hono ( )
@@ -36,6 +37,13 @@ async function handleError(
3637 }
3738
3839 // 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+
3947 return c . json (
4048 {
4149 error : {
@@ -52,18 +60,35 @@ function handleFetchError(
5260 error : FetchError ,
5361) {
5462 const status = error . response ?. status ?? 500
63+ const responseData = error . response ?. _data as unknown
64+ const headers : Record < string , string > = { }
5565
5666 // Forward all headers from the error response
5767 error . response ?. headers . forEach ( ( value , key ) => {
5868 c . header ( key , value )
69+ headers [ key ] = value
5970 } )
6071
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+ )
85+
6186 return c . json (
6287 {
6388 error : {
6489 message : error . message ,
6590 type : "fetch_error" ,
66- data : error . response ?. _data as unknown ,
91+ data : responseData ,
6792 } ,
6893 } ,
6994 status as ContentfulStatusCode ,
@@ -79,11 +104,28 @@ async function handleResponseError(
79104 `Request failed: ${ error . status } ${ error . statusText } : ${ errorText } ` ,
80105 )
81106
107+ const headers : Record < string , string > = { }
108+
82109 // Forward all headers from the error response
83110 error . headers . forEach ( ( value , key ) => {
84111 c . header ( key , value )
112+ headers [ key ] = value
85113 } )
86114
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+
87129 return c . json (
88130 {
89131 error : {
@@ -101,6 +143,14 @@ function handleGenericError(
101143 c : Context < BlankEnv , "/" , BlankInput > ,
102144 error : Error ,
103145) {
146+ // Log the error response
147+ void logger . logResponse ( "/v1/chat/completions" , {
148+ error : {
149+ message : error . message ,
150+ type : "error" ,
151+ } ,
152+ } )
153+
104154 return c . json (
105155 {
106156 error : {
0 commit comments