mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-12 21:53:00 -06:00
refactor: consolidate remaining coercion helpers (#122020)
This commit is contained in:
committed by
GitHub
parent
24bbb416b5
commit
cad77fb39c
@@ -1,6 +1,9 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { truncateUtf16Safe } from "../../../packages/normalization-core/src/utf16-slice.ts";
|
||||
import { toErrorObject as toLintErrorObject } from "../error-format.mts";
|
||||
|
||||
export { toLintErrorObject };
|
||||
|
||||
export function resolveCommandPath(command: string) {
|
||||
const pathValue = process.env.PATH ?? "";
|
||||
@@ -44,17 +47,3 @@ export function sleep(ms: number) {
|
||||
setTimeout(resolvePromise, ms);
|
||||
});
|
||||
}
|
||||
|
||||
export 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;
|
||||
}
|
||||
|
||||
@@ -6,3 +6,23 @@ export function formatErrorMessage(error: unknown): string {
|
||||
}
|
||||
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 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user