Files
openclaw/scripts/lib/error-format.mts
T
Peter Steinberger b080dd1e76 refactor: consolidate coercion contracts (#122458)
* refactor: consolidate coercion contracts

Centralize exact string, record, numeric, date, Boolean, argument, and structured-error coercions while preserving call-site semantics.

Migrate canonical-name collisions and deprecated internal SDK bypasses, deleting 55 net production/tooling lines. Expand declaration ownership enforcement to 101 allowed helpers and add a narrow export-completeness audit.

* fix: preserve standalone script coercions

Keep copied Control UI tooling self-contained and retain the trusted release harness module-relative source seam when the harness runs against an old target cwd.
2026-08-11 23:26:37 -07:00

34 lines
1.2 KiB
TypeScript

// Small error formatting helper for scripts that accept unknown thrown values.
/** Return a readable message for Error and non-Error thrown values. */
export function formatErrorMessage(error: unknown): string {
if (error instanceof Error) {
return error.message || error.name || "Error";
}
return String(error);
}
/** Read Error messages unchanged and stringify every other value. */
export function coerceErrorMessage(value: unknown): string {
return value instanceof Error ? value.message : String(value);
}
/** Preserve Error values and stringify every other value without workspace dependencies. */
export function toStringifiedError(value: unknown): Error {
return value instanceof Error ? value : new Error(String(value));
}
/** Preserve structured non-Error failures without requiring built workspace packages. */
export function toErrorObject(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;
}