2020-05-15 16:23:36 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-06-09 15:40:06 +00:00
|
|
|
import {
|
|
|
|
BalenaAmbiguousApplication,
|
|
|
|
BalenaApplicationNotFound,
|
|
|
|
BalenaDeviceNotFound,
|
|
|
|
BalenaExpiredToken,
|
|
|
|
} from 'balena-errors';
|
2020-05-15 16:23:36 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as sinon from 'sinon';
|
2020-06-22 16:05:10 +00:00
|
|
|
import * as ErrorsModule from '../build/errors';
|
2020-05-15 16:23:36 +00:00
|
|
|
import { getHelp } from '../build/utils/messages';
|
|
|
|
|
|
|
|
function red(s: string) {
|
|
|
|
if (process.env.CI) {
|
|
|
|
// If CI, don't color.
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return `\u001b[31m${s}\u001b[39m`;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('handleError() function', () => {
|
|
|
|
const sandbox = sinon.createSandbox();
|
|
|
|
let printErrorMessage: any;
|
|
|
|
let printExpectedErrorMessage: any;
|
|
|
|
let captureException: any;
|
|
|
|
let processExit: any;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
printErrorMessage = sandbox.stub(ErrorsModule, 'printErrorMessage');
|
|
|
|
printExpectedErrorMessage = sandbox.stub(
|
|
|
|
ErrorsModule,
|
|
|
|
'printExpectedErrorMessage',
|
|
|
|
);
|
|
|
|
captureException = sinon.stub();
|
2023-10-27 14:57:07 +00:00
|
|
|
// @ts-expect-error TODO: get explanation for why this ts-expect-error is necessary
|
2020-05-15 16:23:36 +00:00
|
|
|
sandbox.stub(ErrorsModule, 'getSentry').resolves({ captureException });
|
|
|
|
processExit = sandbox.stub(process, 'exit');
|
|
|
|
|
|
|
|
// Force debug mode off (currently set to true in CI env)
|
|
|
|
sandbox.stub(process, 'env').value({ DEBUG: false });
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should process ExpectedErrors as expected', async () => {
|
|
|
|
const errorMessage = 'an expected error';
|
|
|
|
const error = new ErrorsModule.ExpectedError(errorMessage);
|
|
|
|
|
|
|
|
await ErrorsModule.handleError(error);
|
|
|
|
|
|
|
|
expect(printExpectedErrorMessage.calledOnce).to.be.true;
|
|
|
|
expect(printExpectedErrorMessage.getCall(0).args[0]).to.equal(errorMessage);
|
|
|
|
|
|
|
|
expect(printErrorMessage.notCalled).to.be.true;
|
|
|
|
expect(captureException.notCalled).to.be.true;
|
|
|
|
expect(processExit.notCalled).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should process subclasses of ExpectedErrors as expected', async () => {
|
|
|
|
const errorMessage = 'an expected error';
|
|
|
|
const error = new ErrorsModule.NotLoggedInError(errorMessage);
|
|
|
|
|
|
|
|
await ErrorsModule.handleError(error);
|
|
|
|
|
|
|
|
expect(printExpectedErrorMessage.calledOnce).to.be.true;
|
|
|
|
expect(printExpectedErrorMessage.getCall(0).args[0]).to.equal(errorMessage);
|
|
|
|
|
|
|
|
expect(printErrorMessage.notCalled).to.be.true;
|
|
|
|
expect(captureException.notCalled).to.be.true;
|
|
|
|
expect(processExit.notCalled).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should process unexpected errors correctly (no debug)', async () => {
|
|
|
|
const errorMessage = 'an unexpected error';
|
|
|
|
await ErrorsModule.handleError(new Error(errorMessage));
|
|
|
|
|
|
|
|
expect(printErrorMessage.calledOnce).to.be.true;
|
|
|
|
expect(printErrorMessage.getCall(0).args[0]).to.equal(errorMessage);
|
|
|
|
expect(captureException.calledOnce).to.be.true;
|
|
|
|
expect(processExit.calledOnce).to.be.true;
|
|
|
|
|
|
|
|
expect(printExpectedErrorMessage.notCalled);
|
|
|
|
});
|
|
|
|
|
2021-01-15 15:43:05 +00:00
|
|
|
it('should process thrown strings correctly', async () => {
|
|
|
|
const error = 'an thrown string';
|
|
|
|
await ErrorsModule.handleError(error);
|
|
|
|
|
|
|
|
expect(printErrorMessage.calledOnce).to.be.true;
|
|
|
|
expect(printErrorMessage.getCall(0).args[0]).to.equal(error);
|
|
|
|
expect(captureException.calledOnce).to.be.true;
|
|
|
|
expect(processExit.calledOnce).to.be.true;
|
|
|
|
|
|
|
|
expect(printExpectedErrorMessage.notCalled);
|
|
|
|
});
|
|
|
|
|
2020-05-15 16:23:36 +00:00
|
|
|
it('should process unexpected errors correctly (debug)', async () => {
|
|
|
|
sandbox.stub(process, 'env').value({ DEBUG: true });
|
|
|
|
|
|
|
|
const errorMessage = 'an unexpected error';
|
|
|
|
const error = new Error(errorMessage);
|
|
|
|
await ErrorsModule.handleError(error);
|
|
|
|
|
|
|
|
const expectedMessage = errorMessage + '\n\n' + error.stack;
|
|
|
|
|
|
|
|
expect(printErrorMessage.calledOnce).to.be.true;
|
|
|
|
expect(printErrorMessage.getCall(0).args[0]).to.equal(expectedMessage);
|
|
|
|
expect(captureException.calledOnce).to.be.true;
|
|
|
|
expect(processExit.calledOnce).to.be.true;
|
|
|
|
|
|
|
|
expect(printExpectedErrorMessage.notCalled);
|
|
|
|
});
|
|
|
|
|
|
|
|
const messagesToMatch = [
|
2020-06-17 13:46:25 +00:00
|
|
|
'Missing 1 required argument', // oclif
|
|
|
|
'Missing 2 required arguments', // oclif
|
2020-06-23 10:09:12 +00:00
|
|
|
'Unexpected argument', // oclif
|
|
|
|
'Unexpected arguments', // oclif
|
|
|
|
'to be one of', // oclif
|
|
|
|
'must also be provided when using', // oclif
|
2020-06-24 13:24:54 +00:00
|
|
|
'Expected an integer', // oclif
|
2020-09-25 09:00:18 +00:00
|
|
|
'Flag --foo expects a value', // oclif
|
2020-10-02 14:46:09 +00:00
|
|
|
'BalenaRequestError: Request error: Unauthorized', // sdk
|
2020-05-15 16:23:36 +00:00
|
|
|
];
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
messagesToMatch.forEach((message) => {
|
2020-05-15 16:23:36 +00:00
|
|
|
it(`should match as expected: "${message}"`, async () => {
|
|
|
|
await ErrorsModule.handleError(new Error(message));
|
|
|
|
|
|
|
|
expect(
|
|
|
|
printExpectedErrorMessage.calledOnce,
|
|
|
|
`Pattern not expected: ${message}`,
|
|
|
|
).to.be.true;
|
|
|
|
|
|
|
|
expect(printErrorMessage.notCalled).to.be.true;
|
|
|
|
expect(captureException.notCalled).to.be.true;
|
|
|
|
expect(processExit.notCalled).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
2020-06-09 15:40:06 +00:00
|
|
|
|
|
|
|
const typedErrorsToMatch = [
|
|
|
|
new BalenaAmbiguousApplication('test'),
|
|
|
|
new BalenaApplicationNotFound('test'),
|
|
|
|
new BalenaDeviceNotFound('test'),
|
|
|
|
new BalenaExpiredToken('test'),
|
|
|
|
];
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
typedErrorsToMatch.forEach((typedError) => {
|
2020-06-09 15:40:06 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
});
|
2020-05-15 16:23:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('printErrorMessage() function', () => {
|
|
|
|
it('should correctly output message', () => {
|
|
|
|
const consoleError = sinon.spy(console, 'error');
|
|
|
|
|
|
|
|
const errorMessageLines = [
|
|
|
|
'first line should be red',
|
|
|
|
'second line should not be red',
|
|
|
|
'third line should not be red',
|
|
|
|
];
|
|
|
|
|
|
|
|
const inputMessage = errorMessageLines.join('\n');
|
|
|
|
const expectedOutputMessages = [
|
|
|
|
red(errorMessageLines[0]),
|
|
|
|
errorMessageLines[1],
|
|
|
|
errorMessageLines[2],
|
|
|
|
];
|
|
|
|
|
|
|
|
ErrorsModule.printErrorMessage(inputMessage);
|
|
|
|
|
|
|
|
expect(consoleError.callCount).to.equal(4);
|
|
|
|
expect(consoleError.getCall(0).args[0]).to.equal(expectedOutputMessages[0]);
|
|
|
|
expect(consoleError.getCall(1).args[0]).to.equal(expectedOutputMessages[1]);
|
|
|
|
expect(consoleError.getCall(2).args[0]).to.equal(expectedOutputMessages[2]);
|
2020-11-18 00:34:16 +00:00
|
|
|
expect(consoleError.getCall(3).args[0]).to.equal(`\n${getHelp()}\n`);
|
2020-05-15 16:23:36 +00:00
|
|
|
|
|
|
|
consoleError.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('printExpectedErrorMessage() function', () => {
|
|
|
|
it('should correctly output message', () => {
|
|
|
|
const consoleError = sinon.spy(console, 'error');
|
|
|
|
|
|
|
|
const errorMessage = ['first line', 'second line'].join('\n');
|
|
|
|
|
|
|
|
ErrorsModule.printExpectedErrorMessage(errorMessage);
|
|
|
|
|
|
|
|
expect(consoleError.calledOnce).to.be.true;
|
|
|
|
expect(consoleError.getCall(0).args[0]).to.equal(errorMessage + '\n');
|
|
|
|
|
|
|
|
consoleError.restore();
|
|
|
|
});
|
|
|
|
});
|