diff --git a/lib/errors.ts b/lib/errors.ts index 695e23b3..f399ae7e 100644 --- a/lib/errors.ts +++ b/lib/errors.ts @@ -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 diff --git a/tests/errors.spec.ts b/tests/errors.spec.ts index a0965a22..b6ffd320 100644 --- a/tests/errors.spec.ts +++ b/tests/errors.spec.ts @@ -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 });