Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The page on the error event should explain that errors in promises trigger an unhandledrejection event instead #38246

Open
DavidJCobb opened this issue Feb 20, 2025 · 0 comments
Labels
accepting PR Feel free to open a PR to resolve this issue Content:WebAPI Web API docs

Comments

@DavidJCobb
Copy link
Contributor

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event

What specific section or headline is this issue about?

No response

What information was incorrect, unhelpful, or incomplete?

The article on the error event states that the event is fired when script errors occur, and the article's code example demonstrates this using a script error. However, the article makes no mention of the fact that this only applies to errors in non-async JavaScript functions.

When an error halts execution of an async JavaScript function, this is treated as a rejection of the promise tied to the function's execution, with the error object being the rejection reason. If the async function has no handlers attached, then this triggers an unhandledrejection event. To catch errors that occur in async execution, then, you'd need something like this:

window.addEventListener("unhandledrejection", function(e) {
   if (!(e instanceof Error))
      return;
   // do stuff here
});

What did you expect to see?

  • Up-front clarification of the fact that error is only for synchronous code and unhandledrejection is needed for errors in async code, preferably in one of those little red-colored boxes MDN uses for warnings and caveats.
  • A link to the latter's article in the "See also" list.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

GitHub doesn't allow uploading HTML files, so if you want a quick demonstration, here you go:

<!doctype html>
<html>
   <body>
      <script>
window.addEventListener("error", function(e) {
   console.group("Received ErrorEvent");
   console.log("Error: ", e.error);
   console.groupEnd();
});
window.addEventListener("unhandledrejection", function(e) {
   console.group("Received unhandledrejection event");
   console.log("Reason: ", e.reason);
   console.groupEnd();
});

window.setTimeout(
   function synchronous_thrower() {
      throw new Error("Synchronous error, behind a timeout!");
   },
   1
);

(async function asynchronous_thrower() {
   throw new Error("Asynchronous error!");
})();

window.setTimeout(
   async function synchronous_thrower() {
      throw new Error("Asynchronous error, behind a timeout!");
   },
   1
);

throw new Error("Synchronous error!");
      </script>
   </body>
</html>

MDN metadata

Page report details
@DavidJCobb DavidJCobb added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Feb 20, 2025
@github-actions github-actions bot added the Content:WebAPI Web API docs label Feb 20, 2025
@Josh-Cena Josh-Cena added accepting PR Feel free to open a PR to resolve this issue and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting PR Feel free to open a PR to resolve this issue Content:WebAPI Web API docs
Projects
None yet
Development

No branches or pull requests

2 participants