Skip to content

Commit

Permalink
Add information to getThisFrame error (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Feb 7, 2024
1 parent 2402bcf commit 3fef4fe
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion source/thisTarget.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getContextName,
isBackground,
isContentScript,
isExtensionContext,
Expand Down Expand Up @@ -151,13 +152,21 @@ export function __getTabData(this: MessengerMeta): AnyTarget {
return { tabId: this.trace[0]?.tab?.id, frameId: this.trace[0]?.frameId };
}

// TODO: Add tests

Check warning on line 155 in source/thisTarget.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: Add tests'
export async function getThisFrame(): Promise<FrameTarget> {
await storeTabData(); // It should already have been called but we still need to await it

const { tabId, frameId } = thisTarget;

if (typeof tabId !== "number" || typeof frameId !== "number") {
throw new TypeError("This target is not in a frame");
let moreInfo = "(error retrieving context information)";
try {
moreInfo = `(context: ${getContextName()}, url: ${
globalThis.location?.href
})`;
} catch {}

throw new TypeError(`This target is not in a frame ${moreInfo}`);
}

// Rebuild object to return exactly these two properties and nothing more
Expand Down

0 comments on commit 3fef4fe

Please sign in to comment.