Skip to content

Commit f00f42a

Browse files
committed
refactor(process): share error normalization helper
1 parent af7797b commit f00f42a

3 files changed

Lines changed: 8 additions & 49 deletions

File tree

‎src/node-host/with-timeout.ts‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** Timeout wrapper for node-host operations using AbortSignal cancellation. */
22
import { resolveTimerTimeoutMs } from "@openclaw/normalization-core/number-coercion";
3+
import { toErrorObject } from "../infra/errors.js";
34

45
/**
56
* AbortSignal-based timeout wrapper for node-host operations.
@@ -25,12 +26,10 @@ export async function withTimeout<T>(
2526

2627
let abortListener: (() => void) | undefined;
2728
const abortPromise: Promise<never> = abortCtrl.signal.aborted
28-
? Promise.reject(
29-
toLintErrorObject(abortCtrl.signal.reason ?? timeoutError, "Non-Error rejection"),
30-
)
29+
? Promise.reject(toErrorObject(abortCtrl.signal.reason ?? timeoutError, "Non-Error rejection"))
3130
: new Promise((_, reject) => {
3231
abortListener = () =>
33-
reject(toLintErrorObject(abortCtrl.signal.reason ?? timeoutError, "Non-Error rejection"));
32+
reject(toErrorObject(abortCtrl.signal.reason ?? timeoutError, "Non-Error rejection"));
3433
abortCtrl.signal.addEventListener("abort", abortListener, { once: true });
3534
});
3635

@@ -44,17 +43,3 @@ export async function withTimeout<T>(
4443
}
4544
}
4645
}
47-
48-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
49-
if (value instanceof Error) {
50-
return value;
51-
}
52-
if (typeof value === "string") {
53-
return new Error(value);
54-
}
55-
const error = new Error(fallbackMessage, { cause: value });
56-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
57-
Object.assign(error, value);
58-
}
59-
return error;
60-
}

‎src/process/spawn-utils.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Spawn utilities configure child processes and normalize spawned process handles.
22
import type { ChildProcess, SpawnOptions } from "node:child_process";
33
import { spawn } from "node:child_process";
4+
import { toErrorObject } from "../infra/errors.js";
45

56
export type SpawnFallback = {
67
label: string;
@@ -65,7 +66,7 @@ async function spawnAndWaitForSpawn(
6566
}
6667
settled = true;
6768
cleanup();
68-
reject(toLintErrorObject(err, "Non-Error rejection"));
69+
reject(toErrorObject(err, "Non-Error rejection"));
6970
};
7071
const onSpawn = () => {
7172
finishResolve();
@@ -118,17 +119,3 @@ export async function spawnWithFallback(
118119

119120
throw lastError;
120121
}
121-
122-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
123-
if (value instanceof Error) {
124-
return value;
125-
}
126-
if (typeof value === "string") {
127-
return new Error(value);
128-
}
129-
const error = new Error(fallbackMessage, { cause: value });
130-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
131-
Object.assign(error, value);
132-
}
133-
return error;
134-
}

‎src/process/supervisor/adapters/child.ts‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Child process adapter wraps spawned child processes for the supervisor.
22
import type { ChildProcessWithoutNullStreams, SpawnOptions } from "node:child_process";
3+
import { toErrorObject } from "../../../infra/errors.js";
34
import { createWindowsOutputDecoder } from "../../../infra/windows-encoding.js";
45
import { signalProcessTree } from "../../kill-tree.js";
56
import { prepareOomScoreAdjustedSpawn } from "../../linux-oom-score.js";
@@ -354,7 +355,7 @@ export async function createChildAdapter(params: {
354355
return waitResult;
355356
}
356357
if (waitError !== undefined) {
357-
throw toLintErrorObject(waitError, "Non-Error thrown");
358+
throw toErrorObject(waitError, "Non-Error thrown");
358359
}
359360
if (!waitPromise) {
360361
waitPromise = new Promise<{ code: number | null; signal: NodeJS.Signals | null }>(
@@ -372,7 +373,7 @@ export async function createChildAdapter(params: {
372373
const error = waitError;
373374
resolveWait = null;
374375
rejectWait = null;
375-
reject(toLintErrorObject(error, "Non-Error rejection"));
376+
reject(toErrorObject(error, "Non-Error rejection"));
376377
}
377378
},
378379
);
@@ -433,17 +434,3 @@ export async function createChildAdapter(params: {
433434
dispose,
434435
};
435436
}
436-
437-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
438-
if (value instanceof Error) {
439-
return value;
440-
}
441-
if (typeof value === "string") {
442-
return new Error(value);
443-
}
444-
const error = new Error(fallbackMessage, { cause: value });
445-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
446-
Object.assign(error, value);
447-
}
448-
return error;
449-
}

0 commit comments

Comments
 (0)