From 1560358b3b219d5e9bf2ec7a088093c46e22b437 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Thu, 10 Oct 2024 14:01:59 +0530 Subject: [PATCH] refactor(content/keyAutoAdd): improve timeout handling --- src/content/keyAutoAdd/lib/helpers.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/content/keyAutoAdd/lib/helpers.ts b/src/content/keyAutoAdd/lib/helpers.ts index 6b80375b..4e9ed681 100644 --- a/src/content/keyAutoAdd/lib/helpers.ts +++ b/src/content/keyAutoAdd/lib/helpers.ts @@ -23,7 +23,7 @@ export async function waitForURL( const abortSignal = AbortSignal.timeout(timeout); abortSignal.addEventListener('abort', (e) => { observer.disconnect(); - reject(e); + reject(new TimeoutError(`Timeout waiting for URL`, { cause: e })); }); let url = window.location.href; @@ -43,11 +43,13 @@ export async function waitForURL( return promise; } +class TimeoutError extends Error { + name = 'TimeoutError'; + constructor(message: string, { cause }: { cause: Event }) { + super(message, { cause }); + } +} + export function isTimedOut(e: any) { - return ( - e instanceof Event && - e.type === 'abort' && - e.currentTarget instanceof AbortSignal && - e.currentTarget.reason?.name === 'TimeoutError' - ); + return e instanceof TimeoutError; }