From d3586696b42240e51e428d5ace95d833a25687ea Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Fri, 15 Jan 2021 16:43:05 +0100 Subject: [PATCH] Fix handling of thrown strings Change-type: patch Signed-off-by: Scott Lowe --- lib/errors.ts | 7 ++++++- tests/errors.spec.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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 });