Skip to content

Commit 1c7c2ca

Browse files
author
easonLiangWorldedtech
committed
fix(api): select a numeric status before falling back to statusCode
A null or nonnumeric Error.status was taken as-is, so a nonnumeric value reached Task.backoffAndAnnounce's retry header and a numeric statusCode was ignored. The describe block named for Task integration only exercised the parser directly, so rename it to name the parser as its subject.
1 parent 9d6e0d7 commit 1c7c2ca

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎src/api/providers/utils/__tests__/error-handler.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,38 @@ describe("handleProviderError", () => {
7575
const result = handleProviderError(error, providerName)
7676

7777
expect(result).toBeInstanceOf(Error)
78+
})
79+
80+
it("should prefer a numeric statusCode when status is null", () => {
81+
// A null status is not a status: backoff only understands a number, so the
82+
// numeric statusCode must win.
83+
const error = { message: "Rate limit exceeded", status: null, statusCode: 429 }
84+
const result = handleProviderError(error, providerName)
85+
expect((result as { status?: number }).status).toBe(429)
86+
})
87+
88+
it("should prefer a numeric statusCode when status is a nonnumeric value", () => {
89+
// A truthy nonnumeric status would be echoed into the retry header, so it must
90+
// not win over a numeric statusCode.
91+
const error = new Error("weird")
92+
;(error as { status?: unknown }).status = "429"
93+
;(error as { statusCode?: number }).statusCode = 401
94+
const result = handleProviderError(error, providerName)
95+
expect((result as { status?: number }).status).toBe(401)
96+
})
97+
98+
it("should keep a numeric status and ignore a nonnumeric statusCode", () => {
99+
const error = { message: "weird", status: 503, statusCode: "429" }
100+
const result = handleProviderError(error, providerName)
101+
expect((result as { status?: number }).status).toBe(503)
102+
})
103+
104+
it("should not treat a nonnumeric statusCode as a status on an Error input", () => {
105+
// The typeof guard is what makes this total: an Error instance with a string
106+
// statusCode must not gain a status property.
107+
const error = new Error("weird")
108+
;(error as { statusCode?: unknown }).statusCode = "429"
109+
const result = handleProviderError(error, providerName)
78110
expect(result).not.toHaveProperty("status")
79111
})
80112

‎src/api/providers/utils/error-handler.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ export function handleProviderError(
7878
// to provide status-aware error messages and handling. AI SDK errors (e.g.,
7979
// APICallError) expose the HTTP status as `statusCode` instead of `status`,
8080
// so fall back to it: a real 429 must reach Task.backoffAndAnnounce's .status check.
81-
const preservedStatus = anyErr.status !== undefined ? anyErr.status : anyErr.statusCode
81+
// Only a numeric status can drive backoff: a null or nonnumeric status
82+
// would be echoed straight into the retry header, so take a numeric status
83+
// first and fall back to a numeric statusCode.
84+
const numericStatus = typeof anyErr.status === "number" ? anyErr.status : undefined
85+
const numericStatusCode = typeof anyErr.statusCode === "number" ? anyErr.statusCode : undefined
86+
const preservedStatus = numericStatus !== undefined ? numericStatus : numericStatusCode
8287
if (preservedStatus !== undefined) {
8388
;(wrapped as any).status = preservedStatus
8489
}

‎src/core/task/__tests__/Task.streaming-tool-calls.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ describe("Task - Streaming Tool Call Handling", () => {
630630
})
631631
})
632632

633-
describe("tool_call_partial chunk handling - Task integration", () => {
633+
describe("tool_call_partial chunk handling - NativeToolCallParser raw chunk lifecycle", () => {
634634
it("should emit tool_call_start event when processing raw chunk with id and name", async () => {
635635
NativeToolCallParser.clearRawChunkState(scope)
636636

0 commit comments

Comments
 (0)