diff --git a/src/util/exponentialBackoff.test.ts b/src/util/exponentialBackoff.test.ts index 05fd9f30a..bd0e96c4d 100644 --- a/src/util/exponentialBackoff.test.ts +++ b/src/util/exponentialBackoff.test.ts @@ -13,7 +13,9 @@ function isRetryableError(error: any): boolean { if ( error.code === "ECONNRESET" || error.code === "ENOTFOUND" || - error.code === "ETIMEDOUT" + error.code === "ETIMEDOUT" || + error.code === "EPIPE" || + error.code === "ECONNREFUSED" ) { return true; } @@ -30,6 +32,21 @@ function isRetryableError(error: any): boolean { return true; } + // Anthropic specific errors + const lower = error.message?.toLowerCase(); + if (lower?.includes("overloaded")) { + return true; + } + + // Check for premature close errors by message content + if (lower?.includes("premature close") || + lower?.includes("premature end") || + lower?.includes("connection reset") || + lower?.includes("socket hang up") || + lower?.includes("aborted")) { + return true; + } + return false; } @@ -96,6 +113,51 @@ describe("exponentialBackoff utilities", () => { expect(isRetryableError(error)).toBe(true); }); + it("should return true for EPIPE errors", () => { + const error = { code: "EPIPE" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for ECONNREFUSED errors", () => { + const error = { code: "ECONNREFUSED" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for Anthropic overloaded errors", () => { + const error = { message: "The service is overloaded" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for premature close errors", () => { + const error = { message: "Error: Premature close" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for premature end errors", () => { + const error = { message: "Premature end of response" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for connection reset errors", () => { + const error = { message: "Connection reset by peer" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for socket hang up errors", () => { + const error = { message: "socket hang up" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should return true for aborted connection errors", () => { + const error = { message: "Request aborted" }; + expect(isRetryableError(error)).toBe(true); + }); + + it("should handle case insensitive error messages", () => { + const error = { message: "PREMATURE CLOSE" }; + expect(isRetryableError(error)).toBe(true); + }); + it("should return false for 400 Bad Request", () => { const error = { status: 400 }; expect(isRetryableError(error)).toBe(false); diff --git a/src/util/exponentialBackoff.ts b/src/util/exponentialBackoff.ts index d594b488c..5dc8ead2f 100644 --- a/src/util/exponentialBackoff.ts +++ b/src/util/exponentialBackoff.ts @@ -38,7 +38,9 @@ function isRetryableError(error: any): boolean { if ( error.code === "ECONNRESET" || error.code === "ENOTFOUND" || - error.code === "ETIMEDOUT" + error.code === "ETIMEDOUT" || + error.code === "EPIPE" || + error.code === "ECONNREFUSED" ) { return true; } @@ -61,6 +63,15 @@ function isRetryableError(error: any): boolean { return true; } + // Check for premature close errors by message content + if (lower?.includes("premature close") || + lower?.includes("premature end") || + lower?.includes("connection reset") || + lower?.includes("socket hang up") || + lower?.includes("aborted")) { + return true; + } + return false; }