Fix handling of BalenaExpiredToken error

Change-type: patch
Signed-off-by: Scott Lowe <scott@balena.io>
This commit is contained in:
Scott Lowe 2020-06-09 17:40:06 +02:00
parent a85c482416
commit 555096db6b
2 changed files with 31 additions and 9 deletions

View File

@ -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) {

View File

@ -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', () => {