|
5 | 5 | * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
6 | 6 | */
|
7 | 7 | import { Flags } from '@oclif/core';
|
| 8 | +import { Errors } from '@oclif/core'; |
8 | 9 | import { Lifecycle } from '@salesforce/core';
|
9 | 10 | import { TestContext } from '@salesforce/core/testSetup';
|
10 | 11 | import { assert, expect } from 'chai';
|
@@ -70,12 +71,20 @@ class TestCommandErrors extends SfCommand<void> {
|
70 | 71 | data: errData,
|
71 | 72 | });
|
72 | 73 |
|
| 74 | + public static buildOclifError = () => { |
| 75 | + const err = new Errors.CLIError('Nonexistent flag: --INVALID\nSee more help with --help'); |
| 76 | + err.oclif = { exit: 2 }; |
| 77 | + err.code = undefined; |
| 78 | + return err; |
| 79 | + }; |
| 80 | + |
73 | 81 | // eslint-disable-next-line @typescript-eslint/member-ordering
|
74 | 82 | public static errors: { [x: string]: Error } = {
|
75 | 83 | error: new Error('error message'),
|
76 | 84 | sfError: new SfError('sfError message'),
|
77 | 85 | fullError: TestCommandErrors.buildFullError(),
|
78 | 86 | fullSfError: TestCommandErrors.buildFullSfError(),
|
| 87 | + oclifError: TestCommandErrors.buildOclifError(), |
79 | 88 | };
|
80 | 89 |
|
81 | 90 | // eslint-disable-next-line @typescript-eslint/member-ordering
|
@@ -239,6 +248,38 @@ describe('error standardization', () => {
|
239 | 248 |
|
240 | 249 | afterEach(() => {
|
241 | 250 | process.removeListener('sfCommandError', sfCommandErrorCb);
|
| 251 | + process.exitCode = undefined; |
| 252 | + }); |
| 253 | + |
| 254 | + it('should log correct error when command throws an oclif Error', async () => { |
| 255 | + const logToStderrStub = $$.SANDBOX.stub(SfCommand.prototype, 'logToStderr'); |
| 256 | + try { |
| 257 | + await TestCommandErrors.run(['--error', 'oclifError']); |
| 258 | + expect(false, 'error should have been thrown').to.be.true; |
| 259 | + } catch (e: unknown) { |
| 260 | + expect(e).to.be.instanceOf(SfCommandError); |
| 261 | + const err = e as SfCommand.Error; |
| 262 | + |
| 263 | + // Ensure the error was logged to the console |
| 264 | + expect(logToStderrStub.callCount).to.equal(1); |
| 265 | + expect(logToStderrStub.firstCall.firstArg).to.contain(err.message); |
| 266 | + |
| 267 | + // Ensure the error has expected properties |
| 268 | + expect(err).to.have.property('actions', undefined); |
| 269 | + expect(err).to.have.property('exitCode', 2); |
| 270 | + expect(err).to.have.property('context', 'TestCommandErrors'); |
| 271 | + expect(err).to.have.property('data', undefined); |
| 272 | + expect(err).to.have.property('cause').and.be.ok; |
| 273 | + expect(err).to.have.property('code', '2'); |
| 274 | + expect(err).to.have.property('status', 2); |
| 275 | + expect(err).to.have.property('stack').and.be.ok; |
| 276 | + expect(err).to.have.property('skipOclifErrorHandling', true); |
| 277 | + expect(err).to.have.deep.property('oclif', { exit: 2 }); |
| 278 | + |
| 279 | + // Ensure a sfCommandError event was emitted with the expected data |
| 280 | + expect(sfCommandErrorData[0]).to.equal(err); |
| 281 | + expect(sfCommandErrorData[1]).to.equal('testcommanderrors'); |
| 282 | + } |
242 | 283 | });
|
243 | 284 |
|
244 | 285 | it('should log correct error when command throws an Error', async () => {
|
|
0 commit comments