Skip to content

Commit 596f241

Browse files
authored
fix(app): enhance error handling by unwrapping SDK-wrapped errors in formatServerError (anomalyco#27061)
1 parent 3a810fc commit 596f241

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

packages/app/src/utils/server-errors.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,17 @@ describe("formatServerError", () => {
128128
["Modelo nao encontrado: x/y", "Voce quis dizer: x/y2, x/y3", "Revise provider/model no config"].join("\n"),
129129
)
130130
})
131+
132+
test("unwraps SDK-wrapped errors from cause.body", () => {
133+
const body = {
134+
name: "ConfigInvalidError",
135+
data: {
136+
message: "Missing host",
137+
},
138+
} satisfies ConfigInvalidError
139+
140+
const wrapped = new Error("ConfigInvalidError", { cause: { body, status: 400 } })
141+
142+
expect(formatServerError(wrapped, language.t)).toBe("Arquivo de config em config invalido: Missing host")
143+
})
131144
})

packages/app/src/utils/server-errors.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,22 @@ function tr(translator: Translator | undefined, key: string, text: string, vars?
2626
}
2727

2828
export function formatServerError(error: unknown, translate?: Translator, fallback?: string) {
29-
if (isConfigInvalidErrorLike(error)) return parseReadableConfigInvalidError(error, translate)
30-
if (isProviderModelNotFoundErrorLike(error)) return parseReadableProviderModelNotFoundError(error, translate)
29+
const unwrapped = unwrapNamedError(error)
30+
if (isConfigInvalidErrorLike(unwrapped)) return parseReadableConfigInvalidError(unwrapped, translate)
31+
if (isProviderModelNotFoundErrorLike(unwrapped)) return parseReadableProviderModelNotFoundError(unwrapped, translate)
3132
if (error instanceof Error && error.message) return error.message
3233
if (typeof error === "string" && error) return error
3334
if (fallback) return fallback
3435
return tr(translate, "error.chain.unknown", "Unknown error")
3536
}
3637

38+
function unwrapNamedError(error: unknown): unknown {
39+
if (error instanceof Error && error.cause && typeof error.cause === "object" && "body" in error.cause) {
40+
return (error.cause as Record<string, unknown>).body
41+
}
42+
return error
43+
}
44+
3745
function isConfigInvalidErrorLike(error: unknown): error is ConfigInvalidError {
3846
if (typeof error !== "object" || error === null) return false
3947
const o = error as Record<string, unknown>

0 commit comments

Comments
 (0)