@@ -358,16 +358,28 @@ async function handleChatCompletionsWithBudget(
358358 sameTargetRetries += 1 ;
359359 noteAttemptSend ( logCtx . activeAttempt , logCtx . usageLogInputTokens , "rate-limit-429" ) ;
360360 try {
361- response = await fetchWithHeaderTimeout (
362- url ,
363- { method : "POST" , headers : Object . fromEntries ( upstreamHeaders . entries ( ) ) , body : bodyJson } ,
364- ac . signal ,
365- connectMs ,
366- stream ,
367- providerFetch ( providerConfig ) ,
361+ response = await fetchWithTransientRetry (
362+ ( recovery ) =>
363+ fetchWithHeaderTimeout (
364+ url ,
365+ { method : "POST" , headers : Object . fromEntries ( upstreamHeaders . entries ( ) ) , body : bodyJson , ...( recovery ? { keepalive : false } as unknown as RequestInit : { } ) } ,
366+ ac . signal ,
367+ connectMs ,
368+ stream ,
369+ providerFetch ( providerConfig ) ,
370+ ) ,
371+ { abortSignal : ac . signal , label : safeHostLabel ( url ) } ,
368372 ) ;
369- } catch {
370- break ;
373+ } catch ( err ) {
374+ cleanup ( ) ;
375+ if ( req . signal . aborted || ac . signal . aborted ) {
376+ if ( logIds ) addFinalRequestLog ( logIds . requestId , logIds . start , logCtx , 499 , { closeReason : "client_cancel" } ) ;
377+ return chatCompletionsErrorResponse ( 499 , "Client cancelled request" , "client_cancelled" ) ;
378+ }
379+ const msg = err instanceof Error ? err . message : String ( err ) ;
380+ finishRequestAttempt ( attempt , 502 , Date . now ( ) - ( logCtx . activeAttemptStartedAt ?? Date . now ( ) ) ) ;
381+ if ( logIds ) addFinalRequestLog ( logIds . requestId , logIds . start , logCtx , 502 , { closeReason : "non_stream" } ) ;
382+ return chatCompletionsErrorResponse ( 502 , redactSecretString ( msg ) . slice ( 0 , 500 ) , "server_error" ) ;
371383 }
372384 }
373385 {
@@ -396,16 +408,28 @@ async function handleChatCompletionsWithBudget(
396408 try { void response . body ?. cancel ( ) . catch ( ( ) => { } ) ; } catch { /* noop */ }
397409 noteAttemptSend ( logCtx . activeAttempt , logCtx . usageLogInputTokens , "key-429" ) ;
398410 try {
399- response = await fetchWithHeaderTimeout (
400- url ,
401- { method : "POST" , headers : Object . fromEntries ( upstreamHeaders . entries ( ) ) , body : bodyJson } ,
402- ac . signal ,
403- connectMs ,
404- stream ,
405- providerFetch ( nextProvider ) ,
411+ response = await fetchWithTransientRetry (
412+ ( recovery ) =>
413+ fetchWithHeaderTimeout (
414+ url ,
415+ { method : "POST" , headers : Object . fromEntries ( upstreamHeaders . entries ( ) ) , body : bodyJson , ...( recovery ? { keepalive : false } as unknown as RequestInit : { } ) } ,
416+ ac . signal ,
417+ connectMs ,
418+ stream ,
419+ providerFetch ( nextProvider ) ,
420+ ) ,
421+ { abortSignal : ac . signal , label : safeHostLabel ( url ) } ,
406422 ) ;
407- } catch {
408- break ;
423+ } catch ( err ) {
424+ cleanup ( ) ;
425+ if ( req . signal . aborted || ac . signal . aborted ) {
426+ if ( logIds ) addFinalRequestLog ( logIds . requestId , logIds . start , logCtx , 499 , { closeReason : "client_cancel" } ) ;
427+ return chatCompletionsErrorResponse ( 499 , "Client cancelled request" , "client_cancelled" ) ;
428+ }
429+ const msg = err instanceof Error ? err . message : String ( err ) ;
430+ finishRequestAttempt ( attempt , 502 , Date . now ( ) - ( logCtx . activeAttemptStartedAt ?? Date . now ( ) ) ) ;
431+ if ( logIds ) addFinalRequestLog ( logIds . requestId , logIds . start , logCtx , 502 , { closeReason : "non_stream" } ) ;
432+ return chatCompletionsErrorResponse ( 502 , redactSecretString ( msg ) . slice ( 0 , 500 ) , "server_error" ) ;
409433 }
410434 activeProvider = nextProvider ;
411435 currentKey = nextProvider . apiKey ;
0 commit comments