@@ -12,6 +12,7 @@ import { Storage } from "@/storage/storage"
1212import { ProviderTransform } from "@/provider/transform"
1313import { STATUS_CODES } from "http"
1414import { iife } from "@/util/iife"
15+ import { type SystemError } from "bun"
1516
1617export namespace MessageV2 {
1718 export const OutputLengthError = NamedError . create ( "MessageOutputLengthError" , z . object ( { } ) )
@@ -31,6 +32,7 @@ export namespace MessageV2 {
3132 isRetryable : z . boolean ( ) ,
3233 responseHeaders : z . record ( z . string ( ) , z . string ( ) ) . optional ( ) ,
3334 responseBody : z . string ( ) . optional ( ) ,
35+ details : z . record ( z . string ( ) , z . string ( ) ) . optional ( ) ,
3436 } ) ,
3537 )
3638 export type APIError = z . infer < typeof APIError . Schema >
@@ -609,6 +611,20 @@ export namespace MessageV2 {
609611 } ,
610612 { cause : e } ,
611613 ) . toObject ( )
614+ case ( e as SystemError ) ?. code === "ECONNRESET" &&
615+ ( e as SystemError ) ?. message . includes ( "The socket connection was closed unexpectedly" ) :
616+ return new MessageV2 . APIError (
617+ {
618+ message : "Connection reset by server" ,
619+ isRetryable : true ,
620+ details : {
621+ code : ( e as SystemError ) . code ?? "" ,
622+ syscall : ( e as SystemError ) . syscall ?? "" ,
623+ message : ( e as SystemError ) . message ?? "" ,
624+ } ,
625+ } ,
626+ { cause : e } ,
627+ ) . toObject ( )
612628 case APICallError . isInstance ( e ) :
613629 const message = iife ( ( ) => {
614630 let msg = e . message
0 commit comments