-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreportCliError.mjs
40 lines (34 loc) · 1.18 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
import kleur from "kleur";
import { inspect } from "util";
import CliError from "./CliError.mjs";
import errorConsole from "./errorConsole.mjs";
/**
* Reports a CLI error via the process `stderr`.
* @kind function
* @name reportCliError
* @param {string} cliDescription CLI description.
* @param {*} error Error to report.
* @ignore
*/
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${kleur.bold().red(`Error running ${cliDescription}:`)}\n`
);
errorConsole.error(
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
: inspect(error)
);
errorConsole.groupEnd();
// Whitespace blank line.
errorConsole.error();
}