From b10fedb7de214ffac2b0139494ae66bb4bfe94a4 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 22 Jun 2026 22:45:53 +0800 Subject: [PATCH] refactor(acp): reuse shared error normalization --- src/acp/control-plane/manager.core.ts | 17 ++--------------- .../manager.runtime-resume-state.ts | 18 ++---------------- src/acp/control-plane/manager.utils.ts | 17 ++--------------- 3 files changed, 6 insertions(+), 46 deletions(-) diff --git a/src/acp/control-plane/manager.core.ts b/src/acp/control-plane/manager.core.ts index 6dea9c158f4b..eb1184a6c44d 100644 --- a/src/acp/control-plane/manager.core.ts +++ b/src/acp/control-plane/manager.core.ts @@ -7,6 +7,7 @@ import type { } from "@openclaw/acp-core/runtime/types"; import type { OpenClawConfig } from "../../config/types.openclaw.js"; import { logVerbose } from "../../globals.js"; +import { toErrorObject } from "../../infra/errors.js"; import { isAcpSessionKey } from "../../sessions/session-key-utils.js"; import { AcpRuntimeError } from "../runtime/errors.js"; import { runManagerCancelSession } from "./manager.cancel-session.js"; @@ -584,7 +585,7 @@ export class AcpSessionManager { } settled = true; cleanup(); - reject(toLintErrorObject(error, "Non-Error rejection")); + reject(toErrorObject(error, "Non-Error rejection")); }; const onAbort = () => { if (actorStarted) { @@ -612,17 +613,3 @@ export class AcpSessionManager { throw new AcpRuntimeError("ACP_TURN_FAILED", "ACP operation aborted."); } } - -function toLintErrorObject(value: unknown, fallbackMessage: string): Error { - if (value instanceof Error) { - return value; - } - if (typeof value === "string") { - return new Error(value); - } - const error = new Error(fallbackMessage, { cause: value }); - if ((typeof value === "object" && value !== null) || typeof value === "function") { - Object.assign(error, value); - } - return error; -} diff --git a/src/acp/control-plane/manager.runtime-resume-state.ts b/src/acp/control-plane/manager.runtime-resume-state.ts index eedd32f4bf12..e53a7e425024 100644 --- a/src/acp/control-plane/manager.runtime-resume-state.ts +++ b/src/acp/control-plane/manager.runtime-resume-state.ts @@ -3,7 +3,7 @@ import { resolveSessionIdentityFromMeta } from "@openclaw/acp-core/runtime/sessi import type { AcpRuntime } from "@openclaw/acp-core/runtime/types"; import type { OpenClawConfig } from "../../config/types.openclaw.js"; import { logVerbose } from "../../globals.js"; -import { formatErrorMessage } from "../../infra/errors.js"; +import { formatErrorMessage, toErrorObject } from "../../infra/errors.js"; import type { AcpRuntimeError } from "../runtime/errors.js"; import type { ManagerRuntimeHandleCache } from "./manager.runtime-handle-cache.js"; import type { @@ -188,7 +188,7 @@ export async function tryPrepareFreshManagerRuntimeSession(params: { const backend = params.deps.getRuntimeBackend(configuredBackend || undefined); if (!backend) { if (params.missingBackendError) { - throw toLintErrorObject(params.missingBackendError, "Non-Error thrown"); + throw toErrorObject(params.missingBackendError, "Non-Error thrown"); } return; } @@ -201,17 +201,3 @@ export async function tryPrepareFreshManagerRuntimeSession(params: { ); } } - -function toLintErrorObject(value: unknown, fallbackMessage: string): Error { - if (value instanceof Error) { - return value; - } - if (typeof value === "string") { - return new Error(value); - } - const error = new Error(fallbackMessage, { cause: value }); - if ((typeof value === "object" && value !== null) || typeof value === "function") { - Object.assign(error, value); - } - return error; -} diff --git a/src/acp/control-plane/manager.utils.ts b/src/acp/control-plane/manager.utils.ts index fa77ed2a2525..426f9af39273 100644 --- a/src/acp/control-plane/manager.utils.ts +++ b/src/acp/control-plane/manager.utils.ts @@ -7,6 +7,7 @@ import { } from "../../config/sessions/main-session.js"; import type { SessionAcpMeta } from "../../config/sessions/types.js"; import type { OpenClawConfig } from "../../config/types.openclaw.js"; +import { toErrorObject } from "../../infra/errors.js"; import { normalizeAgentId, normalizeMainKey, @@ -49,7 +50,7 @@ export function requireReadySessionMeta(resolution: AcpSessionResolution): Sessi if (resolution.kind === "ready") { return resolution.meta; } - throw toLintErrorObject(resolveAcpSessionResolutionError(resolution), "Non-Error thrown"); + throw toErrorObject(resolveAcpSessionResolutionError(resolution), "Non-Error thrown"); } function normalizeSessionKey(sessionKey: string): string { @@ -129,17 +130,3 @@ export function hasLegacyAcpIdentityProjection(meta: SessionAcpMeta): boolean { Object.hasOwn(raw, "sessionIdsProvisional") ); } - -function toLintErrorObject(value: unknown, fallbackMessage: string): Error { - if (value instanceof Error) { - return value; - } - if (typeof value === "string") { - return new Error(value); - } - const error = new Error(fallbackMessage, { cause: value }); - if ((typeof value === "object" && value !== null) || typeof value === "function") { - Object.assign(error, value); - } - return error; -}