From aafeba366b1bd9e46d67a0d522cb6ee156fbd536 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sat, 11 Nov 2023 14:26:42 +0800 Subject: [PATCH] Improve error display --- source/global.d.ts | 2 +- source/thisTarget.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/source/global.d.ts b/source/global.d.ts index bf458d3..b78bc98 100644 --- a/source/global.d.ts +++ b/source/global.d.ts @@ -2,6 +2,6 @@ import type { Browser } from "webextension-polyfill"; declare global { const browser: Browser; - // eslint-disable-next-line no-var -- https://stackoverflow.com/a/69208755/288906 + // eslint-disable-next-line no-var -- `let/const` behave differently https://stackoverflow.com/a/69208755/288906 var __webextMessenger: string; } diff --git a/source/thisTarget.ts b/source/thisTarget.ts index 7282d05..42193c0 100644 --- a/source/thisTarget.ts +++ b/source/thisTarget.ts @@ -176,16 +176,16 @@ export function initPrivateApi(): void { // Improve DX by informing the developer that it's being loaded the wrong way // https://github.com/pixiebrix/webext-messenger/issues/88 if (globalThis.__webextMessenger) { + // TODO: Use Error#cause after https://bugs.chromium.org/p/chromium/issues/detail?id=1211260 + console.log(globalThis.__webextMessenger.replace(/^Error: /, "")); console.error( - "webext-messenger has already been imported in this context. This is a fatal error.\nhttps://github.com/pixiebrix/webext-messenger/issues/88", - { - existing: globalThis.__webextMessenger, - current: import.meta.url, - } + "webext-messenger: Duplicate execution. This is a fatal error.\nhttps://github.com/pixiebrix/webext-messenger/issues/88" ); + return; } - globalThis.__webextMessenger = import.meta.url; + // Use Error to capture the stack and make it easier to find the cause + globalThis.__webextMessenger = new Error("First execution").stack!; if (isExtensionContext()) { // Only `runtime` pages can handle this message but I can't remove it because its listener // also serves the purpose of throwing a specific error when no methods have been registered.