diff --git a/lib/errors.ts b/lib/errors.ts index 6880c40d..ac040242 100644 --- a/lib/errors.ts +++ b/lib/errors.ts @@ -135,10 +135,10 @@ const messages: { }; const EXPECTED_ERROR_REGEXES = [ - /^BalenaAmbiguousApplication:/, // balena-sdk - /^BalenaApplicationNotFound:/, // balena-sdk - /^BalenaDeviceNotFound:/, // balena-sdk - /^BalenaExpiredToken:/, // balena-sdk + /^BalenaAmbiguousApplication/, // balena-sdk + /^BalenaApplicationNotFound/, // balena-sdk + /^BalenaDeviceNotFound/, // balena-sdk + /^BalenaExpiredToken/, // balena-sdk /^Missing \w+$/, // Capitano, oclif parser: RequiredArgsError, RequiredFlagError /^Unexpected argument/, // oclif parser: UnexpectedArgsError /to be one of/, // oclif parser: FlagInvalidOptionError, ArgInvalidOptionError @@ -172,7 +172,8 @@ export async function handleError(error: any) { // Expected? const isExpectedError = error instanceof ExpectedError || - EXPECTED_ERROR_REGEXES.some(re => re.test(message[0])); + EXPECTED_ERROR_REGEXES.some(re => re.test(message[0])) || + EXPECTED_ERROR_REGEXES.some(re => re.test((error as any).code)); // Output/report error if (isExpectedError) { diff --git a/tests/errors.spec.ts b/tests/errors.spec.ts index 620edc61..589055f7 100644 --- a/tests/errors.spec.ts +++ b/tests/errors.spec.ts @@ -15,6 +15,12 @@ * limitations under the License. */ +import { + BalenaAmbiguousApplication, + BalenaApplicationNotFound, + BalenaDeviceNotFound, + BalenaExpiredToken, +} from 'balena-errors'; import { expect } from 'chai'; import * as sinon from 'sinon'; import ErrorsModule from '../build/errors'; @@ -124,10 +130,6 @@ describe('handleError() function', () => { }); const messagesToMatch = [ - 'BalenaAmbiguousApplication:', - 'BalenaApplicationNotFound:', - 'BalenaDeviceNotFound:', - 'BalenaExpiredToken:', 'Missing argument', 'Missing arguments', 'Unexpected argument', @@ -149,6 +151,25 @@ describe('handleError() function', () => { expect(processExit.notCalled).to.be.true; }); }); + + const typedErrorsToMatch = [ + new BalenaAmbiguousApplication('test'), + new BalenaApplicationNotFound('test'), + new BalenaDeviceNotFound('test'), + new BalenaExpiredToken('test'), + ]; + + typedErrorsToMatch.forEach(typedError => { + it(`should treat typedError ${typedError.name} as expected`, async () => { + await ErrorsModule.handleError(typedError); + + expect(printExpectedErrorMessage.calledOnce).to.be.true; + + expect(printErrorMessage.notCalled).to.be.true; + expect(captureException.notCalled).to.be.true; + expect(processExit.notCalled).to.be.true; + }); + }); }); describe('printErrorMessage() function', () => {