From a4216b995b1b6c64c15c3e71b7c91b3bbb95c49d Mon Sep 17 00:00:00 2001 From: Wil Wilsman Date: Tue, 8 Sep 2020 13:51:22 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Exit=20after=20logging=20caught?= =?UTF-8?q?=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the error bubbles, it will be logged twice; once by our logger, and once by oclif. --- packages/cli-command/src/command.js | 8 +++++--- packages/cli-command/test/command.test.js | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/cli-command/src/command.js b/packages/cli-command/src/command.js index 435a6e520..b57c7f841 100644 --- a/packages/cli-command/src/command.js +++ b/packages/cli-command/src/command.js @@ -29,9 +29,11 @@ export default class PercyCommand extends Command { // real errors will bubble await super.catch(err); } catch (err) { - // oclif exit method actually throws an error, don't log it - if (!err.oclif || err.code !== 'EEXIT') log.error(err); - throw err; + // oclif exit method actually throws an error, let it continue + if (err.oclif && err.code === 'EEXIT') throw err; + // log all other errors and exit + log.error(err); + this.exit(1); } } diff --git a/packages/cli-command/test/command.test.js b/packages/cli-command/test/command.test.js index 876825a1f..b6610b7fc 100644 --- a/packages/cli-command/test/command.test.js +++ b/packages/cli-command/test/command.test.js @@ -68,14 +68,14 @@ describe('PercyCommand', () => { expect(log.loglevel()).toBe('silent'); }); - it('logs errors to the logger', async () => { + it('logs errors to the logger and exits', async () => { class TestPercyCommandError extends TestPercyCommand { test() { this.error('test error'); } } await expect(stdio.capture(() => ( TestPercyCommandError.run([]) - ))).rejects.toThrow('test error'); + ))).rejects.toThrow('EEXIT: 1'); expect(stdio[1]).toHaveLength(0); expect(stdio[2]).toEqual([