Skip to content

Conversation

@robin-drexler
Copy link
Member

@robin-drexler robin-drexler commented Aug 19, 2025

In polyfilled environments that use realms, it's currently not possible to listen to global errors and unhandled rejections.

onerror / onunhandledrejection handlers

e.g.

window.onerror = (...args) => {
  console.log('### ERROR', args)
};

globalThis.onerror = (...args) => {
  console.log('### ERROR', args)
};

the following works btw. but is not exhaustive enough because libs like sentry and bugsnag attach themselves to window or globalThis

onerror = (...args) => {
  console.log('### ERROR', args)
};

We now make this work by adding event listeners for those events the polyfill window instance that just forwards it to the onerror / onunhandledrejection functions. (thanks @igor10k for that idea!). Those events have been available for years so I think that's ok.

This also doesn't work at the moment inside extensions:

globalThis.addEventListener("error", (...args) => {
  console.log('### ERROR', args);
});

This doesn't work because in realms the globalThis.addEventListener is not the one the worker is dispatching error events to. So we need to to dispatch those events manually from the worker to the realm global.
However we can't dispatch the native event because the polyfill dispatchEvent implementation tries to override target on the event instance, which throws for native events. So we need to also polyfill ErrorEvent and PromiseRejectionEvent which can be dispatched

@robin-drexler
Copy link
Member Author

/snapit

@shopify-github-actions-access
Copy link
Contributor

🫰✨ Thanks @robin-drexler! Your snapshot has been published to npm.

Test the snapshot by updating your package.json with the newly published version:

"@remote-dom/polyfill": "0.0.0-snapshot-20250819185911"

@robin-drexler
Copy link
Member Author

/snapit

@shopify-github-actions-access
Copy link
Contributor

🫰✨ Thanks @robin-drexler! Your snapshot has been published to npm.

Test the snapshot by updating your package.json with the newly published version:

"@remote-dom/polyfill": "0.0.0-snapshot-20250819190822"

MutationObserver = MutationObserver;

get onerror() {
return globalThis.onerror;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unsure if this would lead to infinite calls when we do setGlobal or setGlobalThis, but it worked for me when I tried it.

@robin-drexler robin-drexler marked this pull request as ready for review August 20, 2025 14:47
@robin-drexler robin-drexler force-pushed the rd/fix-error-events branch 2 times, most recently from 9186d8c to 82a63be Compare August 21, 2025 14:06
@robin-drexler
Copy link
Member Author

/snapit

@shopify-github-actions-access
Copy link
Contributor

🫰✨ Thanks @robin-drexler! Your snapshot has been published to npm.

Test the snapshot by updating your package.json with the newly published version:

"@remote-dom/polyfill": "0.0.0-snapshot-20250821140812"

import {HOOKS} from './constants.ts';
import type {Hooks} from './hooks.ts';

type OnErrorHandler =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original on error handler in libdom is typed weirdly in a way where the argument could either be an event object or the 5 args. But in reality it's only the 5 args. https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event#syntax

So we're creating our own type

if (this.#currentOnUnhandledRejectionHandler) {
this.removeEventListener(
'unhandledrejection',
this.#currentOnUnhandledRejectionHandler as any,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think add and remove listener on the polyfill isn't correctly typed. e.g. it doesn't know that unhandledrejection events should have an event object with promise and reason which is causing issues here. type casting as any for now

Copy link
Member

@lemonmade lemonmade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

@lemonmade lemonmade merged commit 8994a49 into main Aug 22, 2025
6 checks passed
Copy link

@waseembahralaseel-cell waseembahralaseel-cell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix my problem too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants