Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 5, 2018
1 parent be1b8d4 commit 5dc1d5e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
35 changes: 17 additions & 18 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ import * as Wrap from 'wrap-ansi'
import {errtermwidth} from './screen'

export class CLIError extends Error {
'cli-ux' = {}
'cli-ux': any
code!: string

constructor(error: string | Error, options: {code?: string, exit?: number} = {}) {
const addExitCode = (error: ExitError) => {
error['cli-ux'] = error['cli-ux'] || {}
error['cli-ux'].exit = options.exit === undefined ? 1 : options.exit
return error
}
if (error instanceof Error) return addExitCode(error as any)
super(error)
addExitCode(this)
this.code = options.code as any
}

render() {
let cli
Expand All @@ -34,23 +47,9 @@ export class ExitError extends CLIError {
'cli-ux': {
exit: number
}
code?: string
code = 'EEXIT'

constructor(error?: string | Error | number, exitCode = 0) {
if (typeof error === 'number') {
exitCode = error
error = undefined
}
const addExitCode = (error: ExitError) => {
error['cli-ux'] = error['cli-ux'] || {}
error['cli-ux'].exit = exitCode
return error
}
if (error instanceof Error) {
return addExitCode(error as any)
}
super(error || `EEXIT: ${exitCode}`)
addExitCode(this)
this.code = 'EEXIT'
constructor(exitCode = 0) {
super(`EEXIT: ${exitCode}`, {exit: exitCode})
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export {Manifest} from './manifest'
export {PJSON} from './pjson'
export {IPlugin, Plugin} from './plugin'
export {Topic} from './topic'
export {ExitError} from './errors'
export {CLIError, ExitError} from './errors'
8 changes: 4 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {inspect} from 'util'

import {Command} from './command'
import Debug from './debug'
import {ExitError} from './errors'
import {CLIError, ExitError} from './errors'
import {Hook, Hooks} from './hooks'
import {Manifest} from './manifest'
import {PJSON} from './pjson'
Expand Down Expand Up @@ -198,8 +198,8 @@ export class Plugin implements IPlugin {
log(message) {
process.stdout.write((message || '') + '\n')
},
error(message, options = {}) {
throw new ExitError(message, options.exit)
error(message, options: {exit?: number} = {}) {
throw new CLIError(message, options)
},
}
const promises = (this.hooks[event] || [])
Expand All @@ -215,7 +215,7 @@ export class Plugin implements IPlugin {

await search(require(p)).call(context, opts)
} catch (err) {
if (err && err['cli-ux'] && err['cli-ux'].exit !== undefined) throw err
if (err && err['cli-ux'] && err['cli-ux']) throw err
process.emitWarning(err)
}
})
Expand Down

0 comments on commit 5dc1d5e

Please sign in to comment.