refactor(errors): extract top-level error handling into shared utility

This commit is contained in:
mitchell 2026-06-17 00:48:15 -04:00
parent 56352d87ec
commit f8e0f9a368
3 changed files with 26 additions and 16 deletions

22
src/errors.ts Normal file
View file

@ -0,0 +1,22 @@
import { chalkStderr } from "chalk";
import wrapAnsi from "wrap-ansi";
export function handleTopLevel(err: unknown) {
if (
typeof err === "object" &&
err !== null &&
"message" in err &&
typeof err.message === "string"
) {
if (process.stderr.isTTY) {
const [columns] = process.stderr.getWindowSize();
console.error(wrapAnsi(chalkStderr.red(err.message), columns));
} else {
console.error(err.message);
}
} else {
console.error(String(err));
}
process.exit(1);
}