refactor(errors): extract top-level error handling into shared utility
This commit is contained in:
parent
56352d87ec
commit
f8e0f9a368
3 changed files with 26 additions and 16 deletions
22
src/errors.ts
Normal file
22
src/errors.ts
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue