From e68ffe846ce5bd15049508e5bce693c8d3b40e4d Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 11 Aug 2025 20:31:04 -0400 Subject: [PATCH 1/4] Add retryable error handling for connection failures and premature close errors --- src/util/exponentialBackoff.test.ts | 64 ++++++++++++++++++++++++++++- src/util/exponentialBackoff.ts | 13 +++++- 2 files changed, 75 insertions(+), 2 deletions(-) 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; } From e6e442ea33d00ad8af7edf582a015d63c34f8126 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 11 Aug 2025 23:08:22 -0400 Subject: [PATCH 2/4] chore: format --- src/util/exponentialBackoff.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/util/exponentialBackoff.ts b/src/util/exponentialBackoff.ts index 5dc8ead2f..552d47ed7 100644 --- a/src/util/exponentialBackoff.ts +++ b/src/util/exponentialBackoff.ts @@ -64,11 +64,13 @@ function isRetryableError(error: any): boolean { } // 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")) { + if ( + lower?.includes("premature close") || + lower?.includes("premature end") || + lower?.includes("connection reset") || + lower?.includes("socket hang up") || + lower?.includes("aborted") + ) { return true; } From 688eb40d88312b6f77294ff541f5cb9adc4b49d5 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 11 Aug 2025 23:12:33 -0400 Subject: [PATCH 3/4] chore: fix complexity --- src/util/exponentialBackoff.ts | 72 ++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/src/util/exponentialBackoff.ts b/src/util/exponentialBackoff.ts index 552d47ed7..b118e9fa7 100644 --- a/src/util/exponentialBackoff.ts +++ b/src/util/exponentialBackoff.ts @@ -30,47 +30,67 @@ const DEFAULT_OPTIONS: Required = { hiddenRetries: 2, }; +/** + * Checks if the error is a network-related error + */ +function isNetworkError(error: any): boolean { + const networkCodes = ["ECONNRESET", "ENOTFOUND", "ETIMEDOUT", "EPIPE", "ECONNREFUSED"]; + return networkCodes.includes(error.code); +} + +/** + * Checks if the HTTP status code is retryable + */ +function isRetryableHttpStatus(status: number): boolean { + // 429 (Too Many Requests), 502 (Bad Gateway), 503 (Service Unavailable), 504 (Gateway Timeout) + return status === 429 || status === 502 || status === 503 || status === 504; +} + +/** + * Checks if the error type indicates a server issue + */ +function isServerError(error: any): boolean { + return error.type === "server_error" || error.type === "rate_limit_exceeded"; +} + +/** + * Checks if the error message indicates a connection issue + */ +function isConnectionError(errorMessage: string): boolean { + const connectionErrorPatterns = [ + "premature close", + "premature end", + "connection reset", + "socket hang up", + "aborted", + "overloaded" + ]; + + return connectionErrorPatterns.some(pattern => errorMessage.includes(pattern)); +} + /** * Determines if an error is retryable based on the error type and status code */ function isRetryableError(error: any): boolean { // Network errors are retryable - if ( - error.code === "ECONNRESET" || - error.code === "ENOTFOUND" || - error.code === "ETIMEDOUT" || - error.code === "EPIPE" || - error.code === "ECONNREFUSED" - ) { + if (isNetworkError(error)) { return true; } // HTTP status codes that are retryable - if (error.status) { - const status = error.status; - // 429 (Too Many Requests), 502 (Bad Gateway), 503 (Service Unavailable), 504 (Gateway Timeout) - return status === 429 || status === 502 || status === 503 || status === 504; - } - - // OpenAI specific errors - if (error.type === "server_error" || error.type === "rate_limit_exceeded") { + if (error.status && isRetryableHttpStatus(error.status)) { return true; } - // Anthropic specific errors - const lower = error.message?.toLowerCase(); - if (lower?.includes("overloaded")) { + // OpenAI/API specific errors + if (isServerError(error)) { 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") - ) { + // Check for connection issues by message content + const errorMessage = error.message?.toLowerCase(); + if (errorMessage && isConnectionError(errorMessage)) { return true; } From 74ebc97c0207e73007f93e4943f4f7f48426e27c Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 11 Aug 2025 23:16:00 -0400 Subject: [PATCH 4/4] chore: format --- src/util/exponentialBackoff.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/util/exponentialBackoff.ts b/src/util/exponentialBackoff.ts index b118e9fa7..cff81418f 100644 --- a/src/util/exponentialBackoff.ts +++ b/src/util/exponentialBackoff.ts @@ -34,7 +34,13 @@ const DEFAULT_OPTIONS: Required = { * Checks if the error is a network-related error */ function isNetworkError(error: any): boolean { - const networkCodes = ["ECONNRESET", "ENOTFOUND", "ETIMEDOUT", "EPIPE", "ECONNREFUSED"]; + const networkCodes = [ + "ECONNRESET", + "ENOTFOUND", + "ETIMEDOUT", + "EPIPE", + "ECONNREFUSED", + ]; return networkCodes.includes(error.code); } @@ -59,14 +65,16 @@ function isServerError(error: any): boolean { function isConnectionError(errorMessage: string): boolean { const connectionErrorPatterns = [ "premature close", - "premature end", + "premature end", "connection reset", "socket hang up", "aborted", - "overloaded" + "overloaded", ]; - - return connectionErrorPatterns.some(pattern => errorMessage.includes(pattern)); + + return connectionErrorPatterns.some((pattern) => + errorMessage.includes(pattern), + ); } /**