@@ -26,6 +26,7 @@ vitest.mock("../../../i18n", () => ({
2626 t : ( key : string ) => key ,
2727} ) )
2828
29+ import type { MockInstance } from "vitest"
2930import OpenAI from "openai"
3031
3132import { zooGatewayDefaultModelId , ZOO_GATEWAY_DEFAULT_TEMPERATURE } from "@roo-code/types"
@@ -500,10 +501,17 @@ describe("ZooGatewayHandler", () => {
500501 } )
501502
502503 describe ( "completePrompt" , ( ) => {
504+ let consoleError : MockInstance < typeof console . error >
505+
503506 beforeEach ( ( ) => {
504507 mockCreate . mockImplementation ( async ( ) => ( {
505508 choices : [ { message : { role : "assistant" , content : "Test completion response" } } ] ,
506509 } ) )
510+ consoleError = vitest . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
511+ } )
512+
513+ afterEach ( ( ) => {
514+ consoleError . mockRestore ( )
507515 } )
508516
509517 it ( "returns completion text from the gateway" , async ( ) => {
@@ -586,6 +594,27 @@ describe("ZooGatewayHandler", () => {
586594 )
587595 } )
588596
597+ it . each ( [
598+ [ "a raw AbortError" , Object . assign ( new Error ( "The user aborted a request" ) , { name : "AbortError" } ) ] ,
599+ [ "the SDK abort error" , new Error ( "Request was aborted." ) ] ,
600+ ] ) (
601+ "normalizes %s from the model lookup to the provider AbortError while the caller signal is live" ,
602+ async ( _label , lookupFailure : Error ) => {
603+ const { getModels, refreshModels } = await import ( "../fetchers/modelCache" )
604+ vitest . mocked ( getModels ) . mockRejectedValue ( lookupFailure )
605+ vitest . mocked ( refreshModels ) . mockRejectedValue ( lookupFailure )
606+ const handler = new ZooGatewayHandler ( mockOptions )
607+
608+ await expect (
609+ handler . completePrompt ( "Test" , { abortSignal : new AbortController ( ) . signal } ) ,
610+ ) . rejects . toMatchObject ( {
611+ name : "AbortError" ,
612+ message : "The Zoo Gateway request was aborted" ,
613+ } )
614+ expect ( mockCreate ) . not . toHaveBeenCalled ( )
615+ } ,
616+ )
617+
589618 it ( "rejects with an AbortError when the timeout alone expires during the SDK call" , async ( ) => {
590619 const handler = new ZooGatewayHandler ( mockOptions )
591620 let sdkSignal : AbortSignal | undefined
@@ -616,6 +645,36 @@ describe("ZooGatewayHandler", () => {
616645 name : "AbortError" ,
617646 message : "The Zoo Gateway request was aborted" ,
618647 } )
648+ // The abort is not a failure of the notification, so nothing is logged.
649+ expect ( consoleError ) . not . toHaveBeenCalled ( )
650+ } )
651+
652+ it . each ( [
653+ [ "with a live caller signal" , true , new Error ( "notification failed" ) , "notification failed" ] ,
654+ [ "without a caller signal" , false , "notification dismissed" , "notification dismissed" ] ,
655+ ] ) (
656+ "logs a failed gateway error notification and still rejects with the gateway error, %s" ,
657+ async ( _label , withSignal : boolean , notificationFailure : unknown , logged : string ) => {
658+ const handler = new ZooGatewayHandler ( mockOptions )
659+ mockCreate . mockRejectedValue ( makeApiError ( 402 , { message : "out of credits" } ) )
660+ showErrorMessage . mockRejectedValue ( notificationFailure )
661+ const options = withSignal ? { abortSignal : new AbortController ( ) . signal } : undefined
662+
663+ await expect ( handler . completePrompt ( "Test" , options ) ) . rejects . toThrow (
664+ "Zoo Gateway completion error: out of credits" ,
665+ )
666+ expect ( consoleError ) . toHaveBeenCalledWith ( "Failed to surface Zoo Gateway error:" , logged )
667+ } ,
668+ )
669+
670+ it ( "rethrows an SDK failure that is not an Error unchanged" , async ( ) => {
671+ const handler = new ZooGatewayHandler ( mockOptions )
672+ const failure = { reason : "upstream failure" }
673+ mockCreate . mockRejectedValue ( failure )
674+
675+ await expect ( handler . completePrompt ( "Test" , { abortSignal : new AbortController ( ) . signal } ) ) . rejects . toBe (
676+ failure ,
677+ )
619678 } )
620679
621680 it ( "rejects with an AbortError when the timeout alone expires during model lookup" , async ( ) => {
@@ -682,6 +741,26 @@ describe("ZooGatewayHandler", () => {
682741 expect ( showErrorMessage ) . not . toHaveBeenCalled ( )
683742 } )
684743
744+ it ( "shows no gateway error for a 402 that rejects just before the caller aborts" , async ( ) => {
745+ const handler = new ZooGatewayHandler ( mockOptions )
746+ const caller = new AbortController ( )
747+ let rejectCompletion : ( reason : unknown ) => void = ( ) => { }
748+ const completion = new Promise ( ( _resolve , reject ) => {
749+ rejectCompletion = reject
750+ } )
751+ mockCreate . mockImplementation ( ( ) => completion )
752+
753+ const pending = handler . completePrompt ( "Test" , { abortSignal : caller . signal } )
754+ await vitest . waitFor ( ( ) => expect ( mockCreate ) . toHaveBeenCalled ( ) )
755+ // This callback runs after the handler's own race settles with the 402 but before completePrompt
756+ // resumes, so only the abort check before the notification keeps the 402 notification away.
757+ completion . catch ( ( ) => caller . abort ( ) )
758+ rejectCompletion ( makeApiError ( 402 ) )
759+
760+ await expect ( pending ) . rejects . toMatchObject ( { name : "AbortError" } )
761+ expect ( showErrorMessage ) . not . toHaveBeenCalled ( )
762+ } )
763+
685764 it ( "does not return a completion that resolves just before the caller aborts" , async ( ) => {
686765 const handler = new ZooGatewayHandler ( mockOptions )
687766 const caller = new AbortController ( )
0 commit comments