UNPKG

889 BPlain TextView Raw
1/**
2 * A custom Error that creates a single-lined message to match current styling inside CLI.
3 * Uses original stack trace when `originalError` is passed or erase the stack if it's not defined.
4 */
5export class CLIError extends Error {
6 constructor(msg: string, originalError?: Error | string) {
7 super(inlineString(msg));
8 if (originalError) {
9 this.stack =
10 typeof originalError === 'string'
11 ? originalError
12 : originalError.stack || ''.split('\n').slice(0, 2).join('\n');
13 } else {
14 // When the "originalError" is not passed, it means that we know exactly
15 // what went wrong and provide means to fix it. In such cases showing the
16 // stack is an unnecessary clutter to the CLI output, hence removing it.
17 delete this.stack;
18 }
19 }
20}
21
22export const inlineString = (str: string) =>
23 str.replace(/(\s{2,})/gm, ' ').trim();