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

Commit

Permalink
Change default handler to use pretty printer
Browse files Browse the repository at this point in the history
  • Loading branch information
chadian committed Jun 4, 2020
1 parent 2258097 commit 1a49ffe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions src/handle.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
/* eslint-disable no-process-exit */
/* eslint-disable unicorn/no-process-exit */

import clean = require('clean-stack')

import {config} from './config'
import prettyPrint, {PrettyPrintableError} from './errors/pretty-print'
import {CLIError, ExitError} from '.'
import clean = require('clean-stack')

export const handle = (err: any) => {
export const handle = (err: Error & PrettyPrintableError | CLIError & PrettyPrintableError) => {
try {
if (!err) err = new Error('no error?')
if (err.message === 'SIGINT') process.exit(1)

const shouldPrint = !(err instanceof ExitError)
const pretty = prettyPrint(err)
const stack = clean(err.stack || '', {pretty: true})
let message = stack
if (err.oclif && typeof err.render === 'function') message = err.render()
if (message) console.error(message)
const exitCode = (err.oclif && err.oclif.exit !== undefined) ? err.oclif.exit : 1

if (shouldPrint) {
console.error(pretty ? pretty : stack)
}
const exitCode = ('oclif' in err && err.oclif.exit !== undefined) ? err.oclif.exit : 1
if (config.errorLogger && err.code !== 'EEXIT') {
config.errorLogger.log(stack)
if (err.stack) {
config.errorLogger.log(stack)
}

config.errorLogger.flush()
.then(() => process.exit(exitCode))
.catch(console.error)
Expand Down
2 changes: 1 addition & 1 deletion test/handle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('handle', () => {
.stderr()
.finally(() => delete process.exitCode)
.it('handles a badly formed error object', () => {
handle({status: 400})
handle({status: 400} as any)
expect(process.exitCode).to.equal(1)
})

Expand Down

0 comments on commit 1a49ffe

Please sign in to comment.