Re: Exceptions in event listeners triggered by dispatchEvent().

On Wed, Jun 5, 2013 at 7:37 AM, Simon Pieters <simonp@opera.com> wrote:

> On Wed, 05 Jun 2013 03:25:12 +0200, Glenn Maynard <glenn@zewt.org> wrote:
>
>  Note that you wouldn't want to call window.onerror directly, since you
>> want
>> "error" event listeners to be run, too.  You'd dispatch an ErrorEvent, eg.
>> window.dispatchEvent(new ErrorEvent("error", {"message": "message",
>> filename: "filename", lineno: 100}));
>>
>
> No. window.onerror is invoked directly without an event. ErrorEvent is for
> the case when an uncaught error is fired on a Worker object (if the error
> wasn't handled in the worker itself).
>

Nope.  In Chrome and IE10, ErrorEvent is used for uncaught exceptions in
the UI thread too, for the "error" event fired on window.  Firefox also
fires "error" event listeners (not just the event handler), but doesn't yet
use ErrorEvent there.

(The spec looks incorrect here.  It bypasses event dispatch entirely and
calls "onerror" directly, which forgets to fire event listeners.  It also
doesn't make much sense to support error handling with event listeners in
workers but not in the UI thread.  This should probably just fire an
ErrorEvent, and the window.onerror special case should probably be in the
event dispatch algorithm.)

If you want to catch an exception and have it behave as though the error
was uncaught and gets sent to onerror, you need to make sure "error" is
dispatched to event listeners.  If you simply call window.onerror directly,
event listeners won't be called.

This works in Chrome, including calling the onerror event handler called
with its special-cased arguments:
http://zewt.org/~glenn/test-error-handler.html.  Firefox doesn't handle
this yet, so you end up with the event handler called with incorrect
arguments.  (IE10 doesn't seem to have any way to create an ErrorEvent
object yet; the interface exists but isn't a constructor.)  I'll file a bug
on HTML.

-- 
Glenn Maynard

Received on Wednesday, 5 June 2013 14:20:20 UTC