fix(errors): tighten duplicate cause formatting

This commit is contained in:
Altay
2026-05-20 20:57:46 +03:00
parent 393f7b70a5
commit 46aa27fa12
2 changed files with 13 additions and 10 deletions

View File

@@ -88,10 +88,11 @@ describe("error helpers", () => {
expect(formatted).toBe("error A | error B");
});
it("dedupes causes whose message repeats the parent (e.g. FailoverError wrapping)", () => {
const inner = new Error('No API key found for provider "openai-codex".');
it("dedupes repeated cause messages while preserving deeper distinct causes", () => {
const rootCause = new Error("provider auth lookup failed");
const inner = new Error('No API key found for provider "openai-codex".', { cause: rootCause });
const wrapper = new Error(inner.message, { cause: inner });
expect(formatErrorMessage(wrapper)).toBe(inner.message);
expect(formatErrorMessage(wrapper)).toBe(`${inner.message} | ${rootCause.message}`);
});
it("redacts sensitive tokens from formatted error messages", () => {

View File

@@ -74,18 +74,20 @@ export function formatErrorMessage(err: unknown): string {
const seen = new Set<unknown>([err]);
// Skip causes that repeat a message already emitted (e.g. coerceToFailoverError).
const seenMessages = new Set<string>([formatted]);
const appendCauseMessage = (message: string): void => {
if (!message || seenMessages.has(message)) {
return;
}
formatted += ` | ${message}`;
seenMessages.add(message);
};
while (cause && !seen.has(cause)) {
seen.add(cause);
if (cause instanceof Error) {
if (cause.message && !seenMessages.has(cause.message)) {
formatted += ` | ${cause.message}`;
seenMessages.add(cause.message);
}
appendCauseMessage(cause.message);
cause = cause.cause;
} else if (typeof cause === "string") {
if (!seenMessages.has(cause)) {
formatted += ` | ${cause}`;
}
appendCauseMessage(cause);
break;
} else {
break;