-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
reportCliError.mjs
41 lines (34 loc) · 1.2 KB
/
reportCliError.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// @ts-check
import { bold, red } from "kleur/colors";
import { inspect } from "node:util";
import CliError from "./CliError.mjs";
import errorConsole from "./errorConsole.mjs";
/**
* Reports a CLI error via the process `stderr`.
* @param {string} cliDescription CLI description.
* @param {unknown} error Error to report.
*/
export default function reportCliError(cliDescription, error) {
if (typeof cliDescription !== "string")
throw new TypeError("Argument 1 `cliDescription` must be a string.");
errorConsole.group(
// Whitespace blank lines shouldn’t have redundant indentation or color.
`\n${bold(red(`Error running ${cliDescription}:`))}\n`
);
errorConsole.error(
red(
error instanceof CliError
? error.message
: error instanceof Error
? // Rarely, an error doesn’t have a stack. In that case, the standard
// `toString` method returns the error’s `name` + `: ` + the
// `message`. This is consistent with the first part of a standard
// Node.js error’s `stack`.
error.stack || error.toString()
: inspect(error)
)
);
errorConsole.groupEnd();
// Whitespace blank line.
errorConsole.error();
}