diff --git a/examples/errors.js b/examples/errors.js deleted file mode 100644 index bba2bf8b..00000000 --- a/examples/errors.js +++ /dev/null @@ -1,3 +0,0 @@ -const cli = require('../lib') - -cli.error('uh oh!') diff --git a/examples/errors.ts b/examples/errors.ts new file mode 100644 index 00000000..abf06bf1 --- /dev/null +++ b/examples/errors.ts @@ -0,0 +1,5 @@ +import cli from '../src' + +cli.warn('uh oh!') +cli.warn('uh oh!', 'myscope') +cli.error('uh oh!') diff --git a/src/errors.ts b/src/errors.ts index c9c7d788..8a1d486a 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -59,15 +59,19 @@ function displayError(err: CLIError) { } function render(): string { - let bang = chalk.red(arrow) - let msg = err['cli-ux'].scope ? `${err['cli-ux'].scope}: ${getErrorMessage(err)}` : getErrorMessage(err) - if (err['cli-ux'].severity === 'fatal') { - bang = chalk.bgRed.bold.white(' FATAL ') - } - if (err['cli-ux'].severity === 'fatal' || config.debug) { - msg += `\n${inspect(err)}` + const {severity, scope} = err['cli-ux'] + if (severity === 'fatal' || config.debug) { + // show stack trace + let msg = '' + if (severity !== 'error') msg += `${severity}: ` + if (scope) msg += `${scope}: ` + msg += err.stack || inspect(err) + return msg } - if (err['cli-ux'].severity === 'warn') bang = chalk.yellow(arrow) + let bang = chalk.red(arrow) + let msg = scope ? `${scope}: ${getErrorMessage(err)}` : getErrorMessage(err) + if (severity as any === 'fatal') bang = chalk.bgRed.bold.white(' FATAL ') + if (severity === 'warn') bang = chalk.yellow(arrow) return bangify(wrap(msg), bang) } @@ -149,15 +153,17 @@ export default (e: IEventEmitter) => { err.code = 'ESIGINT' cli.error(err) }) - process.once('unhandledRejection', handleError('unhandledRejection')) - process.once('uncaughtException', handleError('uncaughtException')) + process.on('unhandledRejection', handleError('unhandledRejection')) + process.on('uncaughtException' as any, handleError('uncaughtException')) process.stdout.on('error', handleError('stdout')) process.stderr.on('error', handleError('stdout')) - e.on('error', handleError('cli-ux')) + e.on('error', handleError('cli-ux') as any) } handleUnhandleds() - return (severity: 'fatal' | 'error' | 'warn', scope?: string) => (input: Error | string, opts: Options = {}) => { + return (severity: 'fatal' | 'error' | 'warn', scope?: string) => (input: Error | string, scopeOrOpts?: string | Options, opts: Options = {}) => { + if (typeof scopeOrOpts === 'string') scope = scopeOrOpts + else if (typeof scopeOrOpts === 'object') opts = scopeOrOpts const error = new CLIError(input, severity, scope, opts) const msg: Message = {type: 'error', scope, severity, error} e.emit('output', msg)