Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
fix: cleaner debug errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 26, 2018
1 parent 5b368de commit 5a731a3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
3 changes: 0 additions & 3 deletions examples/errors.js

This file was deleted.

5 changes: 5 additions & 0 deletions examples/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import cli from '../src'

cli.warn('uh oh!')
cli.warn('uh oh!', 'myscope')
cli.error('uh oh!')
30 changes: 18 additions & 12 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5a731a3

Please sign in to comment.