Skip to content

Commit bcdf40f

Browse files
committed
refactor(plugins): reuse canonical error coercion
1 parent 8f2850d commit bcdf40f

7 files changed

Lines changed: 20 additions & 110 deletions

File tree

‎extensions/acpx/src/runtime-turn.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* ACPX turn adapters. Modern runtimes can expose startTurn directly; legacy
33
* runtimes that only stream runTurn events are adapted to the newer contract.
44
*/
5+
import { toErrorObject } from "openclaw/plugin-sdk/error-runtime";
56
import { createDeferred } from "openclaw/plugin-sdk/extension-shared";
67
import type {
78
AcpRuntime,
@@ -67,7 +68,7 @@ class LegacyRunTurnEventQueue {
6768
return item;
6869
}
6970
if (this.error) {
70-
throw toLintErrorObject(this.error, "Non-Error thrown");
71+
throw toErrorObject(this.error, "Non-Error thrown");
7172
}
7273
if (this.closed) {
7374
return null;
@@ -183,17 +184,3 @@ export function lazyStartRuntimeTurn(
183184
},
184185
};
185186
}
186-
187-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
188-
if (value instanceof Error) {
189-
return value;
190-
}
191-
if (typeof value === "string") {
192-
return new Error(value);
193-
}
194-
const error = new Error(fallbackMessage, { cause: value });
195-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
196-
Object.assign(error, value);
197-
}
198-
return error;
199-
}

‎extensions/copilot/src/event-bridge.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
AgentHarnessAttemptResult,
55
AgentMessage,
66
} from "openclaw/plugin-sdk/agent-harness-runtime";
7+
import { toErrorObject } from "openclaw/plugin-sdk/error-runtime";
78
import {
89
buildAssistantMessage,
910
hasOwnKeys,
@@ -254,7 +255,7 @@ export function attachEventBridge(
254255
});
255256
deltaChain = deltaQueue.then(() => {
256257
if (firstDeltaError !== undefined) {
257-
throw toLintErrorObject(firstDeltaError, "Non-Error thrown");
258+
throw toErrorObject(firstDeltaError, "Non-Error thrown");
258259
}
259260
});
260261
void deltaChain.catch(() => undefined);
@@ -986,17 +987,3 @@ function registerListener<K extends SessionEventType>(
986987
session.off?.(eventType, handler as (...args: unknown[]) => void);
987988
});
988989
}
989-
990-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
991-
if (value instanceof Error) {
992-
return value;
993-
}
994-
if (typeof value === "string") {
995-
return new Error(value);
996-
}
997-
const error = new Error(fallbackMessage, { cause: value });
998-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
999-
Object.assign(error, value);
1000-
}
1001-
return error;
1002-
}

‎extensions/discord/src/monitor.gateway.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Discord plugin module implements monitor.gateway behavior.
2+
import { toErrorObject } from "openclaw/plugin-sdk/error-runtime";
23
import type { DiscordGatewayHandle } from "./monitor/gateway-handle.js";
34
import { DiscordGatewayLifecycleError } from "./monitor/gateway-supervisor.js";
45
import type {
@@ -49,7 +50,7 @@ export async function waitForDiscordGatewayStop(
4950
gateway?.disconnect?.();
5051
} finally {
5152
cleanup();
52-
reject(toLintErrorObject(err, "Non-Error rejection"));
53+
reject(toErrorObject(err, "Non-Error rejection"));
5354
}
5455
};
5556
const onAbort = () => {
@@ -74,17 +75,3 @@ export async function waitForDiscordGatewayStop(
7475
params.registerForceStop?.(onForceStop);
7576
});
7677
}
77-
78-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
79-
if (value instanceof Error) {
80-
return value;
81-
}
82-
if (typeof value === "string") {
83-
return new Error(value);
84-
}
85-
const error = new Error(fallbackMessage, { cause: value });
86-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
87-
Object.assign(error, value);
88-
}
89-
return error;
90-
}

‎extensions/line/src/bot-handlers.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
resolvePairingIdLabel,
1515
upsertChannelPairingRequest,
1616
} from "openclaw/plugin-sdk/conversation-runtime";
17+
import { toErrorObject } from "openclaw/plugin-sdk/error-runtime";
1718
import {
1819
DEFAULT_GROUP_HISTORY_LIMIT,
1920
createChannelHistoryWindow,
@@ -529,7 +530,7 @@ export async function handleLineWebhookEvents(
529530
}
530531
}
531532
if (firstError) {
532-
throw toLintErrorObject(firstError, "Non-Error thrown");
533+
throw toErrorObject(firstError, "Non-Error thrown");
533534
}
534535
}
535536

@@ -560,17 +561,3 @@ async function handleLineWebhookEvent(
560561
logVerbose(`line: unhandled event type: ${(event as WebhookEvent).type}`);
561562
}
562563
}
563-
564-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
565-
if (value instanceof Error) {
566-
return value;
567-
}
568-
if (typeof value === "string") {
569-
return new Error(value);
570-
}
571-
const error = new Error(fallbackMessage, { cause: value });
572-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
573-
Object.assign(error, value);
574-
}
575-
return error;
576-
}

‎extensions/memory-core/src/memory/manager.ts‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import type { DatabaseSync } from "node:sqlite";
33
import type { FSWatcher } from "chokidar";
44
import { resolveAgentConfig } from "openclaw/plugin-sdk/agent-runtime";
5-
import { formatErrorMessage, readErrorName } from "openclaw/plugin-sdk/error-runtime";
5+
import {
6+
formatErrorMessage,
7+
readErrorName,
8+
toErrorObject,
9+
} from "openclaw/plugin-sdk/error-runtime";
610
import { listRegisteredMemoryEmbeddingProviderAdapters } from "openclaw/plugin-sdk/memory-core-host-embedding-registry";
711
import { classifyMemoryMultimodalPath } from "openclaw/plugin-sdk/memory-core-host-engine-embeddings";
812
import {
@@ -972,7 +976,7 @@ export class MemoryIndexManager extends MemoryManagerEmbeddingOps implements Mem
972976
}
973977
}
974978
if (closeFailed) {
975-
throw toLintErrorObject(firstError, "Embedding provider retirement failed");
979+
throw toErrorObject(firstError, "Embedding provider retirement failed");
976980
}
977981
});
978982
this.providerRetirementPromise = retirement;
@@ -2331,7 +2335,7 @@ export class MemoryIndexManager extends MemoryManagerEmbeddingOps implements Mem
23312335
private async retryFailedClose(): Promise<void> {
23322336
const retirementErrors = await this.drainPendingProviderRetirements();
23332337
if (this.providersPendingRetirement.size > 0) {
2334-
throw toLintErrorObject(retirementErrors.at(-1), "Embedding provider retirement failed");
2338+
throw toErrorObject(retirementErrors.at(-1), "Embedding provider retirement failed");
23352339
}
23362340
if (INDEX_CACHE.get(this.cacheKey) === this) {
23372341
INDEX_CACHE.delete(this.cacheKey);
@@ -2446,7 +2450,7 @@ export class MemoryIndexManager extends MemoryManagerEmbeddingOps implements Mem
24462450
(this.providersPendingRetirement.size > 0 ? retirementErrors.at(-1) : undefined) ??
24472451
closeErrors.values().next().value;
24482452
if (closeError) {
2449-
throw toLintErrorObject(closeError, "Non-Error thrown");
2453+
throw toErrorObject(closeError, "Non-Error thrown");
24502454
}
24512455
if (INDEX_CACHE.get(this.cacheKey) === this) {
24522456
INDEX_CACHE.delete(this.cacheKey);
@@ -2461,17 +2465,4 @@ function hasTargetedSessionSyncParams(params: MemorySyncParams | undefined): boo
24612465
);
24622466
}
24632467

2464-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
2465-
if (value instanceof Error) {
2466-
return value;
2467-
}
2468-
if (typeof value === "string") {
2469-
return new Error(value);
2470-
}
2471-
const error = new Error(fallbackMessage, { cause: value });
2472-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
2473-
Object.assign(error, value);
2474-
}
2475-
return error;
2476-
}
24772468
/* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */

‎extensions/qa-channel/src/bus-client.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Qa Channel plugin module implements bus client behavior.
22
import http from "node:http";
33
import https from "node:https";
4+
import { toErrorObject } from "openclaw/plugin-sdk/error-runtime";
45
import { resolvePositiveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
56
import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http";
67
import {
@@ -124,7 +125,7 @@ async function postJson<T>(
124125
resolve(parsed as T);
125126
},
126127
(error: unknown) => {
127-
reject(toLintErrorObject(error, "Non-Error rejection"));
128+
reject(toErrorObject(error, "Non-Error rejection"));
128129
},
129130
);
130131
response.on("error", reject);
@@ -286,17 +287,3 @@ export async function getQaBusState(baseUrl: string): Promise<QaBusStateSnapshot
286287
await release();
287288
}
288289
}
289-
290-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
291-
if (value instanceof Error) {
292-
return value;
293-
}
294-
if (typeof value === "string") {
295-
return new Error(value);
296-
}
297-
const error = new Error(fallbackMessage, { cause: value });
298-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
299-
Object.assign(error, value);
300-
}
301-
return error;
302-
}

‎extensions/whatsapp/src/session.ts‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
} from "baileys";
1111
import { formatCliCommand } from "openclaw/plugin-sdk/cli-runtime";
1212
import { VERSION } from "openclaw/plugin-sdk/cli-runtime";
13+
import { toErrorObject } from "openclaw/plugin-sdk/error-runtime";
1314
import {
1415
createHttp1EnvHttpProxyAgent,
1516
createHttp1ProxyAgent,
@@ -550,10 +551,7 @@ export async function waitForWaConnection(
550551
cleanup();
551552
const disconnectError = update.lastDisconnect?.error ?? update.lastDisconnect;
552553
reject(
553-
toLintErrorObject(
554-
disconnectError ?? new Error("Connection closed"),
555-
"Non-Error rejection",
556-
),
554+
toErrorObject(disconnectError ?? new Error("Connection closed"), "Non-Error rejection"),
557555
);
558556
}
559557
};
@@ -575,20 +573,6 @@ export function newConnectionId() {
575573
return randomUUID();
576574
}
577575

578-
function toLintErrorObject(value: unknown, fallbackMessage: string): Error {
579-
if (value instanceof Error) {
580-
return value;
581-
}
582-
if (typeof value === "string") {
583-
return new Error(value);
584-
}
585-
const error = new Error(fallbackMessage, { cause: value });
586-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
587-
Object.assign(error, value);
588-
}
589-
return error;
590-
}
591-
592576
function createConnectionTimeoutError(timeoutMs: number): Error {
593577
const error = new Error(`WhatsApp connection timed out after ${timeoutMs}ms`);
594578
Object.assign(error, {

0 commit comments

Comments
 (0)