Skip to content

Commit 3a7ec72

Browse files
committed
fix: [#788] surface OpenAI nested error.message instead of Bad Request: {?:?}
`provider/error.ts` extracted `errMsg` via `body.message || body.error || body.error?.message`. For OpenAI's standard `{error: {message, type, code}}` response shape, `body.error` short-circuits as a truthy object, the `typeof errMsg === "string"` guard fails, and the parser falls through to dumping the raw response body. Telemetry (2026-05-04) caught users seeing `APIError: Bad Request: {?:?}` (App Insights redacts string values) and retrying with the same broken model selection because the surfaced error gave no clue about the underlying cause. Switch to an explicit-typeof ternary matching `parseStreamError`'s pattern (line 153). Now handles all three shapes safely: - `{error: {message: "..."}}` (OpenAI) — extracts nested message - `{message: "..."}` (Anthropic et al.) — extracts top-level message - `{error: "..."}` (some others) — extracts string error Wrapped in `altimate_change start — upstream_fix:` markers; upstream has the same bug. Filing upstream PR in parallel. ## Tests Six tests added to `error.test.ts` covering: - OpenAI nested shape extracts the inner message and does not leak the structured body - Top-level message field still works - Plain-string `body.error` still works - Object `body.error` without a `message` key falls through to top-level `body.message` - Non-string `body.error.message` (array/object) does not block a valid `body.message` (regression guard for the same class of bug) - All-non-string-fields body falls through to raw responseBody dump (existing last-resort behavior preserved) Suite: 386/386 provider tests pass. Typecheck clean. Marker check passes. Reviewed by GPT 5.4, Gemini 3.1 Pro, Kimi K2.5 via /consensus:code-review — all approved with the test coverage and ternary improvements that landed before commit. Closes #788
1 parent 10381b0 commit 3a7ec72

2 files changed

Lines changed: 136 additions & 5 deletions

File tree

‎packages/opencode/src/provider/error.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,21 @@ export namespace ProviderError {
6161

6262
try {
6363
const body = JSON.parse(e.responseBody)
64-
// try to extract common error message fields
65-
const errMsg = body.message || body.error || body.error?.message
66-
if (errMsg && typeof errMsg === "string") {
67-
return `${msg}: ${errMsg}`
68-
}
64+
// altimate_change start — upstream_fix: OpenAI errors use {error: {message}} shape;
65+
// the original `body.message || body.error || body.error?.message` short-circuits on
66+
// the parent object, fails the typeof string guard, and dumps the raw body. Use an
67+
// explicit-typeof ternary so a truthy non-string at any level can't block a valid
68+
// string further down the chain (matches parseStreamError's pattern below).
69+
const errMsg =
70+
typeof body.error?.message === "string"
71+
? body.error.message
72+
: typeof body.message === "string"
73+
? body.message
74+
: typeof body.error === "string"
75+
? body.error
76+
: undefined
77+
if (errMsg) return `${msg}: ${errMsg}`
78+
// altimate_change end
6979
} catch {}
7080

7181
// If responseBody is HTML (e.g. from a gateway or proxy error page),

‎packages/opencode/test/provider/error.test.ts‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,125 @@ describe("ProviderError.parseAPICallError: error message extraction", () => {
259259
expect(result.message).toContain("invalid JSON in request body")
260260
}
261261
})
262+
263+
test("extracts nested error.message from OpenAI-shaped JSON body", () => {
264+
// OpenAI returns 4xx errors with {error: {message, type, code}}. The extractor
265+
// must reach body.error.message — not stop at body.error (which is the object).
266+
const result = ProviderError.parseAPICallError({
267+
providerID: "openai" as any,
268+
error: makeAPICallError({
269+
message: "Bad Request",
270+
statusCode: 400,
271+
responseBody: JSON.stringify({
272+
error: {
273+
message: "The model `gpt-5-codex` does not exist or you do not have access to it.",
274+
type: "invalid_request_error",
275+
code: "model_not_found",
276+
},
277+
}),
278+
}),
279+
})
280+
expect(result.type).toBe("api_error")
281+
if (result.type === "api_error") {
282+
expect(result.message).toContain("Bad Request")
283+
expect(result.message).toContain("gpt-5-codex")
284+
expect(result.message).toContain("does not exist")
285+
// The raw structured body must not be dumped when a clean message extracted.
286+
// Detect a leak by looking for JSON delimiters from the parsed body.
287+
expect(result.message).not.toContain('"error":')
288+
expect(result.message).not.toContain("{")
289+
}
290+
})
291+
292+
test("extracts top-level message field when present", () => {
293+
const result = ProviderError.parseAPICallError({
294+
providerID: "anthropic" as any,
295+
error: makeAPICallError({
296+
message: "Bad Request",
297+
statusCode: 400,
298+
responseBody: JSON.stringify({ message: "Field 'foo' is required" }),
299+
}),
300+
})
301+
expect(result.type).toBe("api_error")
302+
if (result.type === "api_error") {
303+
expect(result.message).toContain("Field 'foo' is required")
304+
}
305+
})
306+
307+
test("falls back to body.error string when it is a plain string", () => {
308+
// Some providers return {error: "string"} rather than {error: {message: ...}}.
309+
const result = ProviderError.parseAPICallError({
310+
providerID: "anthropic" as any,
311+
error: makeAPICallError({
312+
message: "Bad Request",
313+
statusCode: 400,
314+
responseBody: JSON.stringify({ error: "Something went wrong" }),
315+
}),
316+
})
317+
expect(result.type).toBe("api_error")
318+
if (result.type === "api_error") {
319+
expect(result.message).toContain("Something went wrong")
320+
}
321+
})
322+
323+
test("falls back through the chain when body.error has no message but body.message does", () => {
324+
// body.error is an object without a `message` key — the extractor must skip it
325+
// and reach the top-level body.message.
326+
const result = ProviderError.parseAPICallError({
327+
providerID: "openai" as any,
328+
error: makeAPICallError({
329+
message: "Bad Request",
330+
statusCode: 400,
331+
responseBody: JSON.stringify({
332+
error: { code: "rate_limited", type: "throttle" },
333+
message: "Slow down",
334+
}),
335+
}),
336+
})
337+
expect(result.type).toBe("api_error")
338+
if (result.type === "api_error") {
339+
expect(result.message).toContain("Slow down")
340+
}
341+
})
342+
343+
test("non-string body.error.message does not block a valid body.message", () => {
344+
// Same class as the bug we just fixed: a truthy non-string at any level of the
345+
// chain must not short-circuit a valid string further down.
346+
const result = ProviderError.parseAPICallError({
347+
providerID: "openai" as any,
348+
error: makeAPICallError({
349+
message: "Bad Request",
350+
statusCode: 400,
351+
responseBody: JSON.stringify({
352+
error: { message: ["array", "of", "strings"] },
353+
message: "Real human-readable error",
354+
}),
355+
}),
356+
})
357+
expect(result.type).toBe("api_error")
358+
if (result.type === "api_error") {
359+
expect(result.message).toContain("Real human-readable error")
360+
expect(result.message).not.toContain("array")
361+
}
362+
})
363+
364+
test("dumps raw body when no string-typed message field exists anywhere", () => {
365+
// body.error has only non-message fields and no top-level message — the parser
366+
// falls through to the raw responseBody dump (last-resort behavior preserved).
367+
const responseBody = JSON.stringify({ error: { code: "x", type: "y" } })
368+
const result = ProviderError.parseAPICallError({
369+
providerID: "openai" as any,
370+
error: makeAPICallError({
371+
message: "Bad Request",
372+
statusCode: 400,
373+
responseBody,
374+
}),
375+
})
376+
expect(result.type).toBe("api_error")
377+
if (result.type === "api_error") {
378+
// Falls through to `${msg}: ${responseBody}` — preserves existing behavior.
379+
expect(result.message).toContain("Bad Request")
380+
expect(result.message).toContain(responseBody)
381+
}
382+
})
262383
})

0 commit comments

Comments
 (0)