MDN self-hosted library · e01.org

WorkerGlobalScope: error event

Web Workers APIworker

Web Workers APIworker

The error event of the WorkerGlobalScope interface fires when an error occurs in the worker.

Syntax

Use the event name in addEventListener():

addEventListener("error", (event) => { })

Alternatively, assign to the event handler property of the interface:

onerror = (message, filename, lineno, colno, error) => { }

[!NOTE] For historical reasons, onerror on Window and WorkerGlobalScope objects is the only event handler property that receives more than one argument.

For more details about this, see the page for the error event on Window objects.

Event type

A generic Event.

Example

The following code snippet shows an onerror handler set inside a worker (but note that this replaces other event listeners added using onerror, if any):

self.onerror = () => {
  console.log("There is an error inside your worker!");
};

The same snippet, but using addEventListener():

self.addEventListener("error", () => {
  console.log("There is an error inside your worker!");
});

Specifications

Browser compatibility

See also

The WorkerGlobalScope interface it belongs to.