Fix handling of thrown strings

Change-type: patch
Signed-off-by: Scott Lowe <scott@balena.io>
This commit is contained in:
Scott Lowe 2021-01-15 16:43:05 +01:00
parent 1f74889386
commit d3586696b4
2 changed files with 18 additions and 1 deletions

View File

@ -219,7 +219,12 @@ async function sentryCaptureException(error: Error) {
}
}
export async function handleError(error: Error) {
export async function handleError(error: Error | string) {
// If a module has thrown a string, convert to error
if (typeof error === 'string') {
error = new Error(error);
}
// Set appropriate exitCode
process.exitCode =
(error as BalenaError).exitCode === 0

View File

@ -99,6 +99,18 @@ describe('handleError() function', () => {
expect(printExpectedErrorMessage.notCalled);
});
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);
});
it('should process unexpected errors correctly (debug)', async () => {
sandbox.stub(process, 'env').value({ DEBUG: true });