@@ -132,9 +132,6 @@ func (b *Bridge) proxyOpenAIRequest(w http.ResponseWriter, r *http.Request) {
132
132
b .logger .Info (r .Context (), "openai request started" , slog .F ("session_id" , sessionID ), slog .F ("method" , r .Method ), slog .F ("path" , r .URL .Path ))
133
133
_ , _ = fmt .Fprintf (os .Stderr , "[%s] new chat session started\n \n " , sessionID )
134
134
135
- // Clear any previous error state
136
- b .clearError ()
137
-
138
135
defer func () {
139
136
b .logger .Info (r .Context (), "openai request ended" , slog .F ("session_id" , sessionID ))
140
137
_ , _ = fmt .Fprintf (os .Stderr , "[%s] chat session ended\n \n " , sessionID )
@@ -321,15 +318,17 @@ func (b *Bridge) proxyOpenAIRequest(w http.ResponseWriter, r *http.Request) {
321
318
}
322
319
323
320
if err := stream .Err (); err != nil {
321
+ b .logger .Error (ctx , "server stream error" , slog .Error (err ))
324
322
var apierr * openai.Error
325
323
if errors .As (err , & apierr ) {
326
- http .Error (w , apierr .Message , apierr .StatusCode )
327
- return
324
+ events .TrySend (ctx , map [string ]interface {}{
325
+ "error" : true ,
326
+ "message" : err .Error (),
327
+ })
328
+ // http.Error(w, apierr.Message, apierr.StatusCode)
329
+ break
328
330
} else if isConnectionError (err ) {
329
- b .logger .Debug (ctx , "upstream connection closed" , slog .Error (err ))
330
- } else {
331
- b .logger .Error (ctx , "server stream error" , slog .Error (err ))
332
- b .setError (err )
331
+ b .logger .Warn (ctx , "upstream connection error" , slog .Error (err ))
333
332
}
334
333
335
334
http .Error (w , err .Error (), http .StatusInternalServerError )
@@ -433,7 +432,6 @@ func (b *Bridge) proxyOpenAIRequest(w http.ResponseWriter, r *http.Request) {
433
432
}
434
433
435
434
b .logger .Error (ctx , "chat completion failed" , slog .Error (err ))
436
- b .setError (err )
437
435
http .Error (w , err .Error (), http .StatusInternalServerError )
438
436
return
439
437
}
@@ -582,9 +580,6 @@ func (b *Bridge) proxyAnthropicRequest(w http.ResponseWriter, r *http.Request) {
582
580
b .logger .Info (r .Context (), "anthropic request started" , slog .F ("session_id" , sessionID ), slog .F ("method" , r .Method ), slog .F ("path" , r .URL .Path ))
583
581
_ , _ = fmt .Fprintf (os .Stderr , "[%s] new chat session started\n \n " , sessionID )
584
582
585
- // Clear any previous error state
586
- b .clearError ()
587
-
588
583
defer func () {
589
584
b .logger .Info (r .Context (), "anthropic request ended" , slog .F ("session_id" , sessionID ))
590
585
_ , _ = fmt .Fprintf (os .Stderr , "[%s] chat session ended\n \n " , sessionID )
@@ -633,7 +628,6 @@ func (b *Bridge) proxyAnthropicRequest(w http.ResponseWriter, r *http.Request) {
633
628
// Policy examples.
634
629
if strings .Contains (string (in .Model ), "opus" ) {
635
630
err := xerrors .Errorf ("%q model is not allowed" , in .Model )
636
- b .setError (err )
637
631
http .Error (w , err .Error (), http .StatusBadRequest )
638
632
return
639
633
}
@@ -1163,10 +1157,6 @@ func (b *Bridge) proxyAnthropicRequest(w http.ResponseWriter, r *http.Request) {
1163
1157
if err != nil {
1164
1158
b .logger .Error (ctx , "failed to send error" , slog .Error (err ))
1165
1159
}
1166
-
1167
- b .setError (antErr )
1168
- } else {
1169
- b .setError (streamErr )
1170
1160
}
1171
1161
}
1172
1162
@@ -1258,45 +1248,6 @@ func (b *Bridge) Addr() string {
1258
1248
return b .addr
1259
1249
}
1260
1250
1261
- // setError sets a structured error with appropriate context
1262
- func (b * Bridge ) setError (val any ) {
1263
- switch err := val .(type ) {
1264
- case error :
1265
- switch {
1266
- case errors .Is (err , context .Canceled ):
1267
- b .lastErr = & BridgeError {
1268
- Code : ErrorTypeRequestCanceled ,
1269
- Message : "Request was canceled" ,
1270
- StatusCode : http .StatusRequestTimeout ,
1271
- }
1272
- case isConnectionError (err ):
1273
- b .lastErr = & BridgeError {
1274
- Code : ErrorTypeConnectionError ,
1275
- Message : "Connection to upstream service failed" ,
1276
- StatusCode : http .StatusBadGateway ,
1277
- }
1278
- default :
1279
- b .lastErr = & BridgeError {
1280
- Code : ErrorTypeUnexpectedError ,
1281
- Message : err .Error (),
1282
- StatusCode : http .StatusInternalServerError ,
1283
- }
1284
- }
1285
- case AnthropicErrorResponse :
1286
- b .lastErr = & BridgeError {
1287
- Code : ErrorTypeAnthropicAPIError ,
1288
- Message : err .Error .Message ,
1289
- Details : map [string ]string {"type" : err .Error .Type },
1290
- StatusCode : err .StatusCode ,
1291
- }
1292
- }
1293
- }
1294
-
1295
- // clearError clears the error state when a new request starts
1296
- func (b * Bridge ) clearError () {
1297
- b .lastErr = nil
1298
- }
1299
-
1300
1251
// logConnectionError logs connection errors with appropriate severity
1301
1252
func (b * Bridge ) logConnectionError (ctx context.Context , err error , operation string ) {
1302
1253
if isConnectionError (err ) {
0 commit comments