Browse documentation

The global object is an EventTarget and reports callback exceptions to its error event

Date: 2026-12-04 Area: runtime Supersedes in part: ADR 0104's deviation on listener exceptions ("an exception thrown by a listener propagates out of dispatchEvent"); ADR 0104's other decisions stand. Refines: ADR 0214: a timer's or a queued callback's exception goes to the global error event first, and is the run's uncaught error only when no listener cancels it. Related: Fetch — EventTarget, Errors, #2596

Context

The global object was not an EventTarget: globalThis.addEventListener, removeEventListener and dispatchEvent did not exist. In a worker and in a window the global scope is an EventTarget, and code written for them registers listeners on it. web-platform-tests use it too: dom/events/EventTarget-removeEventListener.any.js calls globalThis.removeEventListener, and html/webappapis/microtask-queuing/queue-microtask-exceptions.any.js listens to the global error event for a queueMicrotask callback's exception.

ADR 0104 let a listener's exception propagate out of dispatchEvent, because GocciaScript had no global error-reporting channel. The DOM Standard's "inner invoke" (§ 2.9) instead reports it with HTML's "report an exception" (§ 8.1.4.6), which fires a cancelable ErrorEvent named error at the global object. HTML invokes timer handlers and queueMicrotask callbacks with "report" the same way.

WinterTC's Minimum Common Web API (Ecma, 2025 snapshot), § 6 "The global scope", says a runtime's global object should be an instance of EventTarget and that web-interoperable runtimes should follow "report an exception", firing error on the global object; § 5 lists ErrorEvent, reportError() and onerror among the common surface.

An error no listener cancels is, in a browser, written to the developer console. GocciaScript decided in ADR 0214 that a timer's or a queued callback's exception fails the run outside a test runner, as Node and Bun do, and a test runner fails the unit that scheduled it. Deno, which follows the same WinterTC section, reports the exception to the global error event first and ends the process when no listener called preventDefault().

Decision

  • The global object is an EventTarget: it has an EventTarget's internal slots (one listener list for the realm) and EventTarget.prototype is its [[Prototype]], so addEventListener, removeEventListener and dispatchEvent work on it, bare or through globalThis, and globalThis instanceof EventTarget holds.
  • ErrorEvent is a runtime global, with message, filename, lineno, colno and error.
  • "Report an exception" fires a trusted, cancelable ErrorEvent named error at the global object. Its error is the exception; its filename, lineno and colno come from the first located frame of an error's stack; its message reads as a browser's (Uncaught TypeError: …). An exception reported while one is being reported (a throwing error listener) fires no second event, as HTML's error reporting mode has it.
  • The exceptions reported are those of an event listener (DOM inner invoke), a real timer's callback, a queueMicrotask callback, and reportError(e)'s argument. onerror on the global object is HTML's OnErrorEventHandler: it is called with (message, source, lineno, colno, error) and returning true cancels the event.
  • An exception no listener cancels is the run's uncaught error, as Deno has it and as ADR 0214 decided: a timer's or a queued callback's is thrown on where it was before; a listener's or reportError's becomes one once the current job ends. dispatchEvent and controller.abort() no longer throw on a listener's behalf.
  • Fake timers are unchanged: a fake timer's callback is not reported, and its exception is rethrown at the advance that ran it, as Vitest's is.
  • Not covered here: a script's or module's own top-level exception stays the run's result, and the global object fires no unhandledrejection or rejectionhandled events; #2633 tracks the promise rejection events.

Consequences

  • Code written for workers that registers error listeners on the global scope runs unchanged, and can keep a callback's exception from failing the run by canceling the event.
  • The two known failures for #2596 in tests/web-platform-tests/helpers/expectations.js are removed. The web-platform-tests adapter cancels each error event after testharness.js has seen it, as a browser would only write it to its console. negative-setinterval, negative-settimeout, type-long-setinterval and type-long-settimeout in html/webappapis/timers stay left out for another reason: each is a single_test that the worker wrapper's done() completes before its timers run, so it would pass whatever the timers do.
  • Object.prototype.toString.call(globalThis) is [object EventTarget], and globalThis.constructor is EventTarget.