Add retryable error handling for connection failures and premature close errors

This commit is contained in:
Nate 2025-08-11 20:31:04 -04:00
parent 45c1105f7a
commit e68ffe846c
2 changed files with 75 additions and 2 deletions

View file

@ -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);

View file

@ -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;
}