refactor: consolidate remaining coercion helpers (#122020)

This commit is contained in:
Peter Steinberger
2026-08-11 10:22:01 -07:00
committed by GitHub
parent 24bbb416b5
commit cad77fb39c
234 changed files with 1210 additions and 1572 deletions
+3 -14
View File
@@ -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;
}
+20
View File
@@ -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;
}