Skip to content

Commit ffa8360

Browse files
authored
refactor(infra): share error cause reader (#130280)
* refactor(infra): share error cause reader * chore: refresh PR checks
1 parent f7f38bc commit ffa8360

5 files changed

Lines changed: 42 additions & 19 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,7 +3257,7 @@ src/infra/system-run-approval-context.ts 6
32573257
src/infra/tailscale.ts 9
32583258
src/infra/terminal-file-upload.ts 2
32593259
src/infra/tmp-openclaw-dir.ts 3
3260-
src/infra/unhandled-rejections.ts 7
3260+
src/infra/unhandled-rejections.ts 6
32613261
src/infra/update-check-package-target.ts 3
32623262
src/infra/update-doctor-result.ts 2
32633263
src/infra/update-global.ts 3
@@ -3601,7 +3601,7 @@ src/process/supervisor/adapters/pty.ts 1
36013601
src/process/terminal-pty.ts 1
36023602
src/process/windows-command.ts 1
36033603
src/projects/project-registry.ts 1
3604-
src/provider-runtime/operation-retry.ts 4
3604+
src/provider-runtime/operation-retry.ts 3
36053605
src/proxy-capture/env.ts 1
36063606
src/proxy-capture/runtime.ts 10
36073607
src/proxy-capture/store.sqlite.ts 27

‎src/infra/errors.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
hasErrnoCode,
1111
isErrno,
1212
isMissingPathError,
13+
readErrorCause,
1314
readErrorName,
1415
} from "./errors.js";
1516

@@ -37,6 +38,34 @@ describe("error helpers", () => {
3738
expect(readErrorName(value)).toBe(expected);
3839
});
3940

41+
it.each([
42+
["missing cause", {}, undefined],
43+
["undefined cause", { cause: undefined }, undefined],
44+
["null cause", { cause: null }, null],
45+
["arbitrary cause", { cause: "boom" }, "boom"],
46+
["null input", null, undefined],
47+
["primitive input", "boom", undefined],
48+
["function input", Object.assign(() => {}, { cause: "boom" }), undefined],
49+
])("reads %s directly", (_name, value, expected) => {
50+
expect(readErrorCause(value)).toBe(expected);
51+
});
52+
53+
it("preserves self-referential causes", () => {
54+
const error: { cause?: unknown } = {};
55+
error.cause = error;
56+
expect(readErrorCause(error)).toBe(error);
57+
});
58+
59+
it("propagates cause accessor failures", () => {
60+
const failure = new Error("cause access failed");
61+
const error = {
62+
get cause(): never {
63+
throw failure;
64+
},
65+
};
66+
expect(() => readErrorCause(error)).toThrow(failure);
67+
});
68+
4069
it("walks nested error graphs once in breadth-first order", () => {
4170
const leaf = { name: "leaf" };
4271
const child = { name: "child" } as {

‎src/infra/errors.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ export function readErrorName(err: unknown): string {
2525
return typeof name === "string" ? name : "";
2626
}
2727

28+
export function readErrorCause(error: unknown): unknown {
29+
if (!error || typeof error !== "object") {
30+
return undefined;
31+
}
32+
// SAFETY: The object guard permits direct optional cause access without coercion.
33+
return (error as { cause?: unknown }).cause;
34+
}
35+
2836
export function collectErrorGraphCandidates(
2937
err: unknown,
3038
resolveNested?: (current: Record<string, unknown>) => Iterable<unknown>,

‎src/infra/unhandled-rejections.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/st
44
import { restoreRuntimeTerminalState } from "../runtime.js";
55
import { isAbortError } from "./abort-signal.js";
66
import { collectNestedErrorCandidates, extractErrorCodeOrErrno } from "./error-graph-internal.js";
7-
import { extractErrorCode, formatUncaughtError, readErrorName } from "./errors.js";
7+
import { extractErrorCode, formatUncaughtError, readErrorCause, readErrorName } from "./errors.js";
88
import { runFatalErrorHooks } from "./fatal-error-hooks.js";
99
import { isTransientNetworkError } from "./retryable-network-errors.js";
1010

@@ -134,13 +134,6 @@ function isBenignUncaughtNetworkMessage(message: string): boolean {
134134
return message === WS_PRE_HANDSHAKE_CLOSE_MESSAGE;
135135
}
136136

137-
function getErrorCause(err: unknown): unknown {
138-
if (!err || typeof err !== "object") {
139-
return undefined;
140-
}
141-
return (err as { cause?: unknown }).cause;
142-
}
143-
144137
function extractNumericErrorCode(err: unknown, key: "errno" | "errcode"): number | undefined {
145138
if (!err || typeof err !== "object") {
146139
return undefined;
@@ -161,7 +154,7 @@ function extractErrorCodeWithCause(err: unknown): string | undefined {
161154
if (direct) {
162155
return direct;
163156
}
164-
return extractErrorCode(getErrorCause(err));
157+
return extractErrorCode(readErrorCause(err));
165158
}
166159

167160
function isFatalError(err: unknown): boolean {

‎src/provider-runtime/operation-retry.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Provider operation retry helpers run retryable provider operations with backoff.
22
import { sleepWithAbort } from "../infra/backoff.js";
3-
import { formatErrorMessage } from "../infra/errors.js";
3+
import { formatErrorMessage, readErrorCause } from "../infra/errors.js";
44
import { hasRetryableConnectionErrorCode } from "../infra/retryable-network-errors.js";
55

66
export type ProviderOperationRetryStage = "read" | "poll" | "download" | "create";
@@ -97,13 +97,6 @@ function readErrorCode(error: unknown): string | undefined {
9797
return typeof code === "string" ? code : undefined;
9898
}
9999

100-
function readErrorCause(error: unknown): unknown {
101-
if (typeof error !== "object" || error === null) {
102-
return undefined;
103-
}
104-
return (error as { cause?: unknown }).cause;
105-
}
106-
107100
// Provider reads get one bounded retry for negative DNS responses. Gateway
108101
// waits exclude ENOTFOUND because their configured gateway address needs repair.
109102
const PROVIDER_RETRYABLE_DNS_ERROR_CODE_RE = /\bENOTFOUND\b/i;

0 commit comments

Comments
 (0)