Skip to content

Commit

Permalink
feat: add option to skip oclif error printing (#642)
Browse files Browse the repository at this point in the history
* feat: add option to skip oclif error printing

* chore: add uts to cover change

* chore: run test, then push

---------

Co-authored-by: Peter Hale <peter.hale@salesforce.com>
  • Loading branch information
mdonnalley and peternhale committed Feb 28, 2023
1 parent 8c904dc commit ca88895
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/errors/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import clean = require('clean-stack')
import {CLIError} from './errors/cli'
import {OclifError, PrettyPrintableError} from '../interfaces'

export const handle = (err: Error & Partial<PrettyPrintableError> & Partial<OclifError>): void => {
export const handle = (err: Error & Partial<PrettyPrintableError> & Partial<OclifError> & {skipOclifErrorHandling?: boolean}): void => {
try {
if (!err) err = new CLIError('no error?')
if (err.message === 'SIGINT') process.exit(1)

const shouldPrint = !(err instanceof ExitError)
const shouldPrint = !(err instanceof ExitError) && !err.skipOclifErrorHandling
const pretty = prettyPrint(err)
const stack = clean(err.stack || '', {pretty: true})

Expand Down
22 changes: 22 additions & 0 deletions test/errors/handle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ describe('handle', () => {
expect(process.exitCode).to.equal(0)
})

fancy
.stdout()
.stderr()
.it('prints error', ctx => {
const error = new Error('foo bar baz') as Error & {skipOclifErrorHandling: boolean}
error.skipOclifErrorHandling = false
handle(error)
expect(ctx.stdout).to.equal('')
expect(ctx.stderr).to.include('foo bar baz')
})

fancy
.stdout()
.stderr()
.it('should not print error when skipOclifErrorHandling is true', ctx => {
const error = new Error('foo bar baz') as Error & {skipOclifErrorHandling: boolean}
error.skipOclifErrorHandling = true
handle(error)
expect(ctx.stdout).to.equal('')
expect(ctx.stderr).to.equal('')
})

fancy
.stderr()
.do(() => {
Expand Down

0 comments on commit ca88895

Please sign in to comment.