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

window:error event interface and cancelling #24957

Closed
GeorgianStan opened this issue Feb 28, 2023 · 2 comments · Fixed by #25192
Closed

window:error event interface and cancelling #24957

GeorgianStan opened this issue Feb 28, 2023 · 2 comments · Fixed by #25192
Labels
Content:WebAPI Web API docs help wanted If you know something about this topic, we would love your help!

Comments

@GeorgianStan
Copy link

GeorgianStan commented Feb 28, 2023

MDN URL

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

What specific section or headline is this issue about?

event type, usage notes

What information was incorrect, unhelpful, or incomplete?

Event type
Is mentioned that the event type is UIEvent.

Usage notes
In usage notes is mentioned:

Unlike other events, the error event is canceled by returning true from the handler instead of returning false. When canceled, the error won't appear in the console, but the current script will still stop executing.

What did you expect to see?

Event type*
If we run this code we'll see in the console that the error is of type ErrorEvent.
In HTML spec is mentioned the same.

window.addEventListener("error", (event) => {
  console.log(event); // event is of type ErrorEvent
});

const badCode = "const s;";
eval(badCode);

Usage notes

The return statement doesn't seem to have an impact, Regardless of the returned value: return true or return false the error message is displayed in the console. This event it looks like is similar with unhandledrejection and event.preventDefault() must be used, so that the message is not printed to the console.

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

discussions page

@GeorgianStan GeorgianStan added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Feb 28, 2023
@github-actions github-actions bot added the Content:WebAPI Web API docs label Feb 28, 2023
@sideshowbarker sideshowbarker added help wanted If you know something about this topic, we would love your help! and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Mar 3, 2023
@wbamberg
Copy link
Collaborator

wbamberg commented Mar 4, 2023

As far as I can tell: the bit about UIEvent looks just wrong and it should be ErrorEvent.

The bit about cancelling the event is a bit more complicated. I think:

  • if you listen for an event using addEventListener, yes, you cancel it using preventDefault(). And that works fine for Window's error event as well.
  • but if you listen using the on- event handler property, then most of these can also cancel by returning false. Try this:
<textarea>Type in me!</textarea>
document.querySelector("textarea").onkeydown = (e) => {
	console.log("cancelled!");
	return false;
};

Except that for Window's onerror handler, this is reversed, and you have to return true to cancel:

<button id="error">Error!</button>
window.onerror = () => {
	console.log("cancelled!");
	return true;
};

The spec also discusses this: https://html.spec.whatwg.org/multipage/webappapis.html#eventhandler.
This is so weird that it definitely seems worth documenting, but that page should be clearer that it is only talking about the event handler property (onerror) here.

Does that make sense?

@GeorgianStan
Copy link
Author

Therefore, looks like UIEvent should be replaced with ErrorEvent and the documentation should be extended to cover both ways of canceling the event: return true for .onerror and preventDefault() for addEventListener.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs help wanted If you know something about this topic, we would love your help!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants