From c4d98638b1e83118fcbfc66f7a71c60807f9a926 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Wed, 14 Sep 2022 17:38:19 -0500 Subject: [PATCH] firmware/actions: fix error type check we need to check that it matches the error interface, not that it is an instance of an error --- src/firmware/actions.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/firmware/actions.ts b/src/firmware/actions.ts index 9d058d08..b18baa84 100644 --- a/src/firmware/actions.ts +++ b/src/firmware/actions.ts @@ -155,6 +155,18 @@ export const didFinish = createAction(() => ({ type: 'flashFirmware.action.didFinish', })); +function isError(err: unknown): err is Error { + const maybeError = err as Error; + + return ( + maybeError !== undefined && + typeof maybeError.name === 'string' && + typeof maybeError.message === 'string' + ); +} + +// FIXME: get rid of this monstrosity + const didFailToFinishType = 'flashFirmware.action.didFailToFinish'; function didFailToFinishCreator(reason: FailToFinishReasonType.FailedToConnect): { @@ -260,7 +272,7 @@ function didFailToFinishCreator( } { if (reason === FailToFinishReasonType.BleError) { // istanbul ignore if: programmer error give wrong arg - if (!(arg1 instanceof Error)) { + if (!isError(arg1)) { throw new Error('missing or invalid err'); } return { @@ -328,7 +340,7 @@ function didFailToFinishCreator( if (reason === FailToFinishReasonType.Unknown) { // istanbul ignore if: programmer error give wrong arg - if (!(arg1 instanceof Error)) { + if (!isError(arg1)) { throw new Error('missing or invalid err'); } return {