mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 15:30:39 +00:00
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
export function extractErrorCode(err: unknown): string | undefined {
|
|
if (!err || typeof err !== "object") {
|
|
return undefined;
|
|
}
|
|
const code = (err as { code?: unknown }).code;
|
|
if (typeof code === "string") {
|
|
return code;
|
|
}
|
|
if (typeof code === "number") {
|
|
return String(code);
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* Type guard for NodeJS.ErrnoException (any error with a `code` property).
|
|
*/
|
|
export function isErrno(err: unknown): err is NodeJS.ErrnoException {
|
|
return Boolean(err && typeof err === "object" && "code" in err);
|
|
}
|
|
|
|
/**
|
|
* Check if an error has a specific errno code.
|
|
*/
|
|
export function hasErrnoCode(err: unknown, code: string): boolean {
|
|
return isErrno(err) && err.code === code;
|
|
}
|
|
|
|
export function formatErrorMessage(err: unknown): string {
|
|
if (err instanceof Error) {
|
|
return err.message || err.name || "Error";
|
|
}
|
|
if (typeof err === "string") {
|
|
return err;
|
|
}
|
|
if (typeof err === "number" || typeof err === "boolean" || typeof err === "bigint") {
|
|
return String(err);
|
|
}
|
|
try {
|
|
return JSON.stringify(err);
|
|
} catch {
|
|
return Object.prototype.toString.call(err);
|
|
}
|
|
}
|
|
|
|
export function formatUncaughtError(err: unknown): string {
|
|
if (extractErrorCode(err) === "INVALID_CONFIG") {
|
|
return formatErrorMessage(err);
|
|
}
|
|
if (err instanceof Error) {
|
|
return err.stack ?? err.message ?? err.name;
|
|
}
|
|
return formatErrorMessage(err);
|
|
}
|