UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

228 lines (227 loc) • 8.56 kB
import "./number-coercion-PJEgwXYS.mjs"; import { hasNodeErrorCode as hasErrnoCode, isNodeError as isErrno } from "@openclaw/fs-safe/path"; //#region packages/normalization-core/src/error-coercion.ts const PROVIDER_AUTH_PERSISTENCE_ERROR_BRAND = Symbol.for("openclaw.provider-auth-persistence-error"); function isProviderAuthPersistenceError(value) { try { return PROVIDER_AUTH_PERSISTENCE_ERROR_BRAND in value && value[PROVIDER_AUTH_PERSISTENCE_ERROR_BRAND] === true; } catch { return false; } } function isErrorObject(value) { try { if (value instanceof Error) return true; return Object.prototype.toString.call(value) === "[object Error]"; } catch { return false; } } function isAggregateErrorObject(error) { try { if (error instanceof AggregateError) return true; for (let proto = Object.getPrototypeOf(error); proto; proto = Object.getPrototypeOf(proto)) { const constructor = Object.getOwnPropertyDescriptor(proto, "constructor")?.value; if (typeof constructor === "function" && constructor.name === "AggregateError") return true; } } catch {} return false; } function readProperty(value, key) { try { return value[key]; } catch { return; } } function readErrorText(value, key) { const field = readProperty(value, key); return typeof field === "string" ? field : void 0; } function formatStatusAndCode(value) { if ((typeof value !== "object" || value === null) && typeof value !== "function") return; try { if (Object.keys(value).some((key) => key !== "status" && key !== "code")) return; } catch {} const statusValue = readProperty(value, "status"); const codeValue = readProperty(value, "code"); if (statusValue === void 0 && codeValue === void 0) return; return `status=${typeof statusValue === "string" || typeof statusValue === "number" ? String(statusValue) : "unknown"} code=${typeof codeValue === "string" || typeof codeValue === "number" ? String(codeValue) : "unknown"}`; } function stringifyUnknown(value) { if (value === null || typeof value !== "object" && typeof value !== "function") return String(value); try { const json = JSON.stringify(value); if (json !== void 0) return json; } catch {} try { return Object.prototype.toString.call(value); } catch { return "Unknown error"; } } function readErrorCauses(current) { if (!isErrorObject(current)) return []; const cause = readProperty(current, "cause"); const errors = isAggregateErrorObject(current) ? readProperty(current, "errors") : void 0; const persistenceFailures = isProviderAuthPersistenceError(current) ? [readProperty(current, "persistenceError"), readProperty(current, "cleanupError")] : []; const suppressed = readErrorText(current, "name") === "SuppressedError" ? [readProperty(current, "error"), readProperty(current, "suppressed")].map((failure) => failure == null ? String(failure) : failure) : []; return [ ...persistenceFailures, cause || void 0, ...Array.isArray(errors) ? errors : [], ...suppressed ]; } /** Formats unknown errors with cause/aggregate details, structured codes, and secret redaction. */ function formatErrorMessage(value, options) { let formatted; if (isErrorObject(value)) { formatted = readErrorText(value, "message") || readErrorText(value, "name") || "Error"; const seenMessages = /* @__PURE__ */ new Set([formatted]); const appendCauseMessage = (message) => { if (!message || seenMessages.has(message)) return; formatted += ` | ${message}`; seenMessages.add(message); }; if (options.includeCode) { const code = readProperty(value, "code"); if (typeof code === "string" || typeof code === "number") appendCauseMessage(String(code)); } const causes = collectErrorGraphCandidates(value, readErrorCauses); for (const cause of causes.slice(1)) if (isErrorObject(cause)) { const message = readErrorText(cause, "message"); if (message && formatted.includes(message)) seenMessages.add(message); else appendCauseMessage(message); const code = readProperty(cause, "code"); if (typeof code === "string" || typeof code === "number") appendCauseMessage(String(code)); } else if (typeof cause === "string") appendCauseMessage(cause); else appendCauseMessage(formatStatusAndCode(cause) ?? stringifyUnknown(cause)); } else formatted = formatStatusAndCode(value) ?? stringifyUnknown(value); return options.redact(formatted); } /** * Normalizes an unknown thrown value into an Error. Non-Error objects become * the `cause` and have their enumerable fields copied so structured details * (codes, statuses) survive the coercion. */ function toErrorObject(value, fallbackMessage) { 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; } /** Reads Error messages unchanged and stringifies every other value. */ function coerceErrorMessage(value) { return value instanceof Error ? value.message : String(value); } function extractErrorCode(err) { if (!err || typeof err !== "object") return; const code = readProperty(err, "code"); return typeof code === "string" || typeof code === "number" ? String(code) : void 0; } function collectErrorGraphCandidates(err, resolveNested) { if (err == null) return []; const candidates = [err]; const seen = (/* @__PURE__ */ new Set()).add(err); for (const current of candidates) { if (!current || typeof current !== "object" || !resolveNested) continue; for (const nested of resolveNested(current)) if (nested != null && !seen.has(nested)) { seen.add(nested); candidates.push(nested); } } return candidates; } function extractErrorCodeOrErrno(err) { const code = extractErrorCode(err); if (code) return code.trim().toUpperCase(); if (!err || typeof err !== "object") return; const errno = err.errno; if (typeof errno === "string" && errno.trim()) return errno.trim().toUpperCase(); if (typeof errno === "number" && Number.isFinite(errno)) return String(errno); } function collectNestedErrorCandidates(err) { return collectErrorGraphCandidates(err, (current) => { const nested = [ readProperty(current, "cause"), readProperty(current, "reason"), readProperty(current, "original"), readProperty(current, "error"), readProperty(current, "suppressed"), readProperty(current, "data") ]; const errors = readProperty(current, "errors"); if (Array.isArray(errors)) nested.push(...errors); return nested; }); } //#endregion //#region packages/normalization-core/src/cjk-chars.ts const WEIGHTED_CJK_RANGES = [ [ [183, 183], [12288, 12703], [19968, 40869], [44032, 55215], [65281, 65376] ], [ [4352, 4607], [11904, 12287], [12704, 19967], [40870, 40959], [40960, 42239], [42752, 42759], [43360, 43391], [55216, 55295], [63744, 64255] ], [ [711, 711], [713, 715], [729, 729], [746, 747], [773, 773], [803, 803], [65040, 65103], [65377, 65500], [65504, 65510] ], [[119648, 119665]], [ [94176, 94207], [110576, 110591], [110592, 110959], [127488, 127743], [131072, 195103], [196608, 210047] ] ]; new RegExp(`[${WEIGHTED_CJK_RANGES.flatMap((ranges) => ranges.map(([start, end]) => `\\u{${start.toString(16)}}-\\u{${end.toString(16)}}`)).join("")}]`, "u"); (() => { const maxCodePoint = Math.max(...WEIGHTED_CJK_RANGES.flatMap((ranges) => ranges.map(([, end]) => end))); const categories = new Uint8Array(maxCodePoint + 1); for (const [bucket, ranges] of WEIGHTED_CJK_RANGES.entries()) for (const [start, end] of ranges) categories.fill(bucket + 1, start, end + 1); return categories; })(); //#endregion //#region src/infra/prototype-keys.ts const BLOCKED_OBJECT_KEYS = /* @__PURE__ */ new Set([ "__proto__", "prototype", "constructor" ]); /** Return true when assigning `key` could mutate an object prototype. */ function isBlockedObjectKey(key) { return BLOCKED_OBJECT_KEYS.has(key); } //#endregion //#region src/infra/errno.ts /** Classifies missing filesystem paths across Node and fs-safe boundaries. */ function isMissingPathError(err) { return hasErrnoCode(err, "ENOENT") || hasErrnoCode(err, "ENOTDIR") || hasErrnoCode(err, "not-found"); } //#endregion export { coerceErrorMessage as a, extractErrorCode as c, toErrorObject as d, isBlockedObjectKey as i, extractErrorCodeOrErrno as l, isErrno as n, collectErrorGraphCandidates as o, isMissingPathError as r, collectNestedErrorCandidates as s, hasErrnoCode as t, formatErrorMessage as u };