From d998849b2941dbf56b7b675e636037c0d942caaf Mon Sep 17 00:00:00 2001 From: Justin <57725347+juskek@users.noreply.github.com> Date: Fri, 21 Jul 2023 12:58:18 +0100 Subject: [PATCH] refactor: type cast error instead of conditional check --- src/background/background.ts | 20 +++++++++----------- src/background/backgroundHelpers.ts | 18 +++++++++--------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/background/background.ts b/src/background/background.ts index c16f7948..f93fe9cb 100644 --- a/src/background/background.ts +++ b/src/background/background.ts @@ -31,17 +31,15 @@ chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => { try { await chrome.debugger.detach({ tabId: tabId }); } catch (e: unknown) { - if (e instanceof Error) { - if ( - e.message === - `Debugger is not attached to the tab with id: ${tabId}.` - ) { - console.warn( - `Tried to detach debugger from tab (tabId: ${tabId}) when there was none attached. ` - ); - return; - } - throw e; + const error = e as Error; + if ( + error.message === + `Debugger is not attached to the tab with id: ${tabId}.` + ) { + console.warn( + `Tried to detach debugger from tab (tabId: ${tabId}) when there was none attached. ` + ); + return; } throw e; } diff --git a/src/background/backgroundHelpers.ts b/src/background/backgroundHelpers.ts index 9d9bb32b..bd1af154 100644 --- a/src/background/backgroundHelpers.ts +++ b/src/background/backgroundHelpers.ts @@ -11,15 +11,15 @@ export const addBytesTransferred = async (bytes: number) => { }, }); } catch (e: unknown) { - if (e instanceof Error) { - if ( - e.message === - "Could not establish connection. Receiving end does not exist." - ) { - console.warn( - `Error Caught: ${e}\nIf popup is open and this error is seen in the console, debugging is required.` - ); - } + const error = e as Error; + + if ( + error.message === + "Could not establish connection. Receiving end does not exist." + ) { + console.warn( + `Error Caught: ${e}\nIf popup is open and this error is seen in the console, debugging is required.` + ); } else { throw e; }