Skip to content

Commit e9d4bf8

Browse files
fix: handle errors without stack traces when using --verbose (#796)
* handle errors without stack traces when using --verbose * remove test log
1 parent d1741cf commit e9d4bf8

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/BaseCommand.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class BaseCommand extends Command {
3535
async catch (error) {
3636
const { flags } = await this.parse(this.prototype)
3737
aioLogger.error(error) // debug log
38-
this.error(flags.verbose ? error.stack : error.message)
38+
this.error(flags.verbose && error.stack ? error.stack : error.message)
3939
}
4040

4141
async init () {

test/BaseCommand.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,26 @@ test('will change error message when aio app outside of the application root (--
302302
expect(cmd.error).toHaveBeenCalledWith(expect.stringContaining(errorList.join('\n')))
303303
})
304304

305+
test('will handle errors without stack traces when using --verbose flag', async () => {
306+
const cmd = new TheCommand(['--verbose'])
307+
cmd.error = jest.fn()
308+
const errorWithoutStack = new Error('fake error')
309+
delete errorWithoutStack.stack
310+
await cmd.catch(errorWithoutStack)
311+
312+
expect(cmd.error).toHaveBeenCalledWith(expect.stringContaining('fake error'))
313+
})
314+
315+
test('will handle errors without stack traces when not using --verbose flag', async () => {
316+
const cmd = new TheCommand([])
317+
cmd.error = jest.fn()
318+
const errorWithoutStack = new Error('fake error')
319+
delete errorWithoutStack.stack
320+
await cmd.catch(errorWithoutStack)
321+
322+
expect(cmd.error).toHaveBeenCalledWith(expect.stringContaining('fake error'))
323+
})
324+
305325
test('pjson', async () => {
306326
const cmd = new TheCommand([])
307327
cmd.config = { pjson: { name: 'fake', version: '0' } }

0 commit comments

Comments
 (0)