Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 92a3aa8

Browse files
authored
Merge pull request #190 from microsoft/emimunoz/command
Handling oclif/error CLIError in base class
2 parents 7c760dc + 771c566 commit 92a3aa8

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

packages/command/src/command.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { Command as Base } from '@oclif/command'
44
export { flags } from '@oclif/command'
5+
import { CLIError as OCLIFError } from '@oclif/errors'
56
import { CLIError } from './clierror'
67
export { CLIError } from './clierror'
78
import ReadPipedData from './readpipeddata'
@@ -33,7 +34,7 @@ export abstract class Command extends Base {
3334
}
3435

3536
async catch(err: any) {
36-
if (err instanceof CLIError) {
37+
if (err instanceof CLIError || err instanceof OCLIFError) {
3738
if (!err.message.match(/EEXIT: 0/)) {
3839
this.error(err.message)
3940
}
@@ -71,7 +72,7 @@ export abstract class Command extends Base {
7172
}
7273

7374
private extractError(input: string | Error): string {
74-
return input instanceof Error ? input.message.concat(input.name) : input
75+
return input instanceof Error ? input.name : input
7576
}
7677

7778
private getTelemetryProperties(): Array<string> {

packages/command/test/command.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Command} from '../src/command'
22
import {CLIError} from './../src/clierror'
3+
import { CLIError as OCLIFError } from '@oclif/errors'
34
import {expect, fancy} from 'fancy-test'
45
import ReadPipedStdin from '../src/readpipeddata'
56
import * as path from 'path';
@@ -36,6 +37,20 @@ describe('command', () => {
3637
.do(output => expect(output.stderr).to.equal('failure\n'))
3738
.it('Exits with error')
3839

40+
fancy
41+
.stderr()
42+
.do(async () => {
43+
class Test extends Command {
44+
async run() {
45+
throw new OCLIFError('failure')
46+
}
47+
}
48+
49+
return Test.run([])
50+
})
51+
.do(output => expect(output.stderr).to.equal('failure\n'))
52+
.it('Handles OCLIF Errors')
53+
3954
fancy
4055
.stderr()
4156
.do(async () => {

0 commit comments

Comments
 (0)