Skip to content

Commit

Permalink
🐛 Exit after logging caught errors
Browse files Browse the repository at this point in the history
If the error bubbles, it will be logged twice; once by our logger, and once by oclif.
  • Loading branch information
wwilsman committed Sep 8, 2020
1 parent 3657a3f commit a4216b9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/cli-command/src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cli-command/test/command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit a4216b9

Please sign in to comment.