diff --git a/extensions/matrix/src/matrix/sdk/idb-persistence.test-helpers.ts b/extensions/matrix/src/matrix/sdk/idb-persistence.test-helpers.ts index 9bd2fb16c37d..93e94135c932 100644 --- a/extensions/matrix/src/matrix/sdk/idb-persistence.test-helpers.ts +++ b/extensions/matrix/src/matrix/sdk/idb-persistence.test-helpers.ts @@ -1,4 +1,6 @@ // Matrix helper module supports idb persistence helpers behavior. +import { toErrorObject } from "openclaw/plugin-sdk/error-runtime"; + export async function clearAllIndexedDbState(params?: { databasePrefix?: string }): Promise { const databases = await indexedDB.databases(); const expectedPrefix = params?.databasePrefix ? `${params.databasePrefix}::` : null; @@ -14,7 +16,7 @@ export async function clearAllIndexedDbState(params?: { databasePrefix?: string req.addEventListener("success", () => resolve(), { once: true }); req.addEventListener( "error", - () => reject(toLintErrorObject(req.error, "Non-Error rejection")), + () => reject(toErrorObject(req.error, "Non-Error rejection")), { once: true }, ); req.addEventListener("blocked", () => resolve(), { once: true }); @@ -48,17 +50,13 @@ export async function seedDatabase(params: { db.close(); resolve(); }); - tx.addEventListener( - "error", - () => reject(toLintErrorObject(tx.error, "Non-Error rejection")), - { once: true }, - ); + tx.addEventListener("error", () => reject(toErrorObject(tx.error, "Non-Error rejection")), { + once: true, + }); + }); + req.addEventListener("error", () => reject(toErrorObject(req.error, "Non-Error rejection")), { + once: true, }); - req.addEventListener( - "error", - () => reject(toLintErrorObject(req.error, "Non-Error rejection")), - { once: true }, - ); }); } @@ -97,33 +95,17 @@ export async function readDatabaseRecords(params: { }); keysReq.addEventListener( "error", - () => reject(toLintErrorObject(keysReq.error, "Non-Error rejection")), + () => reject(toErrorObject(keysReq.error, "Non-Error rejection")), { once: true }, ); valuesReq.addEventListener( "error", - () => reject(toLintErrorObject(valuesReq.error, "Non-Error rejection")), + () => reject(toErrorObject(valuesReq.error, "Non-Error rejection")), { once: true }, ); }); - req.addEventListener( - "error", - () => reject(toLintErrorObject(req.error, "Non-Error rejection")), - { once: true }, - ); + req.addEventListener("error", () => reject(toErrorObject(req.error, "Non-Error rejection")), { + once: true, + }); }); } - -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/extensions/matrix/src/matrix/sdk/idb-persistence.ts b/extensions/matrix/src/matrix/sdk/idb-persistence.ts index 57818ee401af..048d31943470 100644 --- a/extensions/matrix/src/matrix/sdk/idb-persistence.ts +++ b/extensions/matrix/src/matrix/sdk/idb-persistence.ts @@ -2,6 +2,7 @@ import fs from "node:fs"; import path from "node:path"; import { indexedDB as fakeIndexedDB } from "fake-indexeddb"; +import { toErrorObject } from "openclaw/plugin-sdk/error-runtime"; import { withFileLock } from "openclaw/plugin-sdk/file-lock"; import { MATRIX_IDB_SNAPSHOT_FILENAME, @@ -133,11 +134,9 @@ export function isValidMatrixIdbSnapshotJson(data: string): boolean { function idbReq(req: IDBRequest): Promise { return new Promise((resolve, reject) => { req.addEventListener("success", () => resolve(req.result), { once: true }); - req.addEventListener( - "error", - () => reject(toLintErrorObject(req.error, "Non-Error rejection")), - { once: true }, - ); + req.addEventListener("error", () => reject(toErrorObject(req.error, "Non-Error rejection")), { + once: true, + }); }); } @@ -157,7 +156,7 @@ async function dumpIndexedDatabases(databasePrefix?: string): Promise { const r = idb.open(name, version); r.addEventListener("success", () => resolve(r.result), { once: true }); - r.addEventListener("error", () => reject(toLintErrorObject(r.error, "Non-Error rejection")), { + r.addEventListener("error", () => reject(toErrorObject(r.error, "Non-Error rejection")), { once: true, }); }); @@ -245,7 +244,7 @@ async function restoreIndexedDatabases(snapshot: IdbDatabaseSnapshot[]): Promise }, { once: true }, ); - r.addEventListener("error", () => reject(toLintErrorObject(r.error, "Non-Error rejection")), { + r.addEventListener("error", () => reject(toErrorObject(r.error, "Non-Error rejection")), { once: true, }); }); @@ -390,17 +389,3 @@ function throwLegacySnapshotMigrationRequired(): never { LogService.warn("IdbPersistence", LEGACY_SNAPSHOT_DIAGNOSTIC); throw new MatrixIdbSnapshotMigrationRequiredError(); } - -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/extensions/matrix/src/matrix/startup-abort.ts b/extensions/matrix/src/matrix/startup-abort.ts index a357d26ad342..214ce0c9c5d3 100644 --- a/extensions/matrix/src/matrix/startup-abort.ts +++ b/extensions/matrix/src/matrix/startup-abort.ts @@ -1,4 +1,6 @@ // Matrix plugin module implements startup abort behavior. +import { toErrorObject } from "openclaw/plugin-sdk/error-runtime"; + export function createMatrixStartupAbortError(): Error { const error = new Error("Matrix startup aborted"); error.name = "AbortError"; @@ -38,22 +40,8 @@ export async function awaitMatrixStartupWithAbort( }, (error: unknown) => { abortSignal.removeEventListener("abort", onAbort); - reject(toLintErrorObject(error, "Non-Error rejection")); + reject(toErrorObject(error, "Non-Error rejection")); }, ); }); } - -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; -}