refactor(matrix): reuse shared error coercion (#113586)

This commit is contained in:
Vincent Koc
2026-07-25 16:54:55 +08:00
committed by GitHub
parent 48ab15851b
commit e4d6170237
3 changed files with 23 additions and 68 deletions

View File

@@ -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<void> {
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;
}

View File

@@ -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<T>(req: IDBRequest<T>): Promise<T> {
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<IdbDatabas
const db: IDBDatabase = await new Promise((resolve, reject) => {
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;
}

View File

@@ -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<T>(
},
(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;
}