refactor: dedupe core error helpers

This commit is contained in:
Peter Steinberger
2026-04-07 03:55:26 +01:00
parent e169fcd263
commit a03e430248
6 changed files with 17 additions and 63 deletions

View File

@@ -1,5 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import { formatErrorMessage } from "../infra/errors.js";
export { isRecord } from "../utils.js";
export function isNonEmptyString(value: unknown): value is string {
@@ -61,22 +62,5 @@ export function writeTextFileAtomic(pathname: string, value: string, mode = 0o60
}
export function describeUnknownError(err: unknown): string {
if (err instanceof Error && err.message.trim().length > 0) {
return err.message;
}
if (typeof err === "string" && err.trim().length > 0) {
return err;
}
if (typeof err === "number" || typeof err === "bigint") {
return err.toString();
}
if (typeof err === "boolean") {
return err ? "true" : "false";
}
try {
const serialized = JSON.stringify(err);
return serialized ?? "unknown error";
} catch {
return "unknown error";
}
return formatErrorMessage(err);
}