* fix: treat HTTP 503 as failover-eligible for LLM provider errors
When LLM SDKs wrap 503 responses, the leading "503" prefix is lost
(e.g. Google Gemini returns "high demand" / "UNAVAILABLE" without a
numeric prefix). The existing isTransientHttpError only matches
messages starting with "503 ...", so these wrapped errors silently
skip failover — no profile rotation, no model fallback.
This patch closes that gap:
- resolveFailoverReasonFromError: map HTTP status 503 → rate_limit
(covers structured error objects with a status field)
- ERROR_PATTERNS.overloaded: add /\b503\b/, "service unavailable",
"high demand" (covers message-only classification when the leading
status prefix is absent)
Existing isTransientHttpError behavior is unchanged; these additions
are complementary and only fire for errors that previously fell
through unclassified.
* fix: address review feedback — drop /\b503\b/ pattern, add test coverage
- Remove `/\b503\b/` from ERROR_PATTERNS.overloaded to resolve the
semantic inconsistency noted by reviewers: `isTransientHttpError`
already handles messages prefixed with "503" (→ "timeout"), so a
redundant overloaded pattern would classify the same class of errors
differently depending on message formatting.
- Keep "service unavailable" and "high demand" patterns — these are the
real gap-fillers for SDK-rewritten messages that lack a numeric prefix.
- Add test case for JSON-wrapped 503 error body containing "overloaded"
to strengthen coverage.
* fix: unify 503 classification — status 503 → timeout (consistent with isTransientHttpError)
resolveFailoverReasonFromError previously mapped status 503 → "rate_limit",
while the string-based isTransientHttpError mapped "503 ..." → "timeout".
Align both paths: structured {status: 503} now also returns "timeout",
matching the existing transient-error convention. Both reasons are
failover-eligible, so runtime behavior is unchanged.
---------
Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
75 lines
2.7 KiB
TypeScript
75 lines
2.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
coerceToFailoverError,
|
|
describeFailoverError,
|
|
isTimeoutError,
|
|
resolveFailoverReasonFromError,
|
|
} from "./failover-error.js";
|
|
|
|
describe("failover-error", () => {
|
|
it("infers failover reason from HTTP status", () => {
|
|
expect(resolveFailoverReasonFromError({ status: 402 })).toBe("billing");
|
|
expect(resolveFailoverReasonFromError({ statusCode: "429" })).toBe("rate_limit");
|
|
expect(resolveFailoverReasonFromError({ status: 403 })).toBe("auth");
|
|
expect(resolveFailoverReasonFromError({ status: 408 })).toBe("timeout");
|
|
expect(resolveFailoverReasonFromError({ status: 400 })).toBe("format");
|
|
expect(resolveFailoverReasonFromError({ status: 503 })).toBe("timeout");
|
|
});
|
|
|
|
it("infers format errors from error messages", () => {
|
|
expect(
|
|
resolveFailoverReasonFromError({
|
|
message: "invalid request format: messages.1.content.1.tool_use.id",
|
|
}),
|
|
).toBe("format");
|
|
});
|
|
|
|
it("infers timeout from common node error codes", () => {
|
|
expect(resolveFailoverReasonFromError({ code: "ETIMEDOUT" })).toBe("timeout");
|
|
expect(resolveFailoverReasonFromError({ code: "ECONNRESET" })).toBe("timeout");
|
|
});
|
|
|
|
it("infers timeout from abort stop-reason messages", () => {
|
|
expect(resolveFailoverReasonFromError({ message: "Unhandled stop reason: abort" })).toBe(
|
|
"timeout",
|
|
);
|
|
expect(resolveFailoverReasonFromError({ message: "stop reason: abort" })).toBe("timeout");
|
|
expect(resolveFailoverReasonFromError({ message: "reason: abort" })).toBe("timeout");
|
|
});
|
|
|
|
it("treats AbortError reason=abort as timeout", () => {
|
|
const err = Object.assign(new Error("aborted"), {
|
|
name: "AbortError",
|
|
reason: "reason: abort",
|
|
});
|
|
expect(isTimeoutError(err)).toBe(true);
|
|
});
|
|
|
|
it("coerces failover-worthy errors into FailoverError with metadata", () => {
|
|
const err = coerceToFailoverError("credit balance too low", {
|
|
provider: "anthropic",
|
|
model: "claude-opus-4-5",
|
|
});
|
|
expect(err?.name).toBe("FailoverError");
|
|
expect(err?.reason).toBe("billing");
|
|
expect(err?.status).toBe(402);
|
|
expect(err?.provider).toBe("anthropic");
|
|
expect(err?.model).toBe("claude-opus-4-5");
|
|
});
|
|
|
|
it("coerces format errors with a 400 status", () => {
|
|
const err = coerceToFailoverError("invalid request format", {
|
|
provider: "google",
|
|
model: "cloud-code-assist",
|
|
});
|
|
expect(err?.reason).toBe("format");
|
|
expect(err?.status).toBe(400);
|
|
});
|
|
|
|
it("describes non-Error values consistently", () => {
|
|
const described = describeFailoverError(123);
|
|
expect(described.message).toBe("123");
|
|
expect(described.reason).toBeUndefined();
|
|
});
|
|
});
|