UNPKG

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