UNPKG

1.25 kBJavaScriptView Raw
1import { inspect } from 'util';
2import kleur from 'kleur';
3import CliError from './CliError.mjs';
4import errorConsole from './errorConsole.mjs';
5
6/**
7 * Reports a CLI error via the process `stderr`.
8 * @kind function
9 * @name reportCliError
10 * @param {string} cliDescription CLI description.
11 * @param {*} error Error to report.
12 * @ignore
13 */
14export default function reportCliError(cliDescription, error) {
15 if (typeof cliDescription !== 'string')
16 throw new TypeError('First argument `cliDescription` must be a string.');
17
18 errorConsole.group(
19 // Whitespace blank lines shouldn’t have redundant indentation or color.
20 `\n${kleur.bold().red(`Error running ${cliDescription}:`)}\n`
21 );
22
23 errorConsole.error(
24 kleur.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 `message`.
30 // This is consistent with the first part of a standard Node.js
31 // error’s `stack`.
32 error.stack || error
33 : inspect(error)
34 )
35 );
36
37 errorConsole.groupEnd();
38
39 // Whitespace blank line.
40 errorConsole.error();
41}