WorkerGlobalScope: error event
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is only available in Web Workers.
The error event of the WorkerGlobalScope interface fires when an error occurs in the worker.
Syntax
Use the event name in addEventListener():
js
addEventListener("error", (event) => { })
Alternatively, assign to the event handler property of the interface:
js
onerror = (message, filename, lineno, colno, error) => { }
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):
js
self.onerror = () => {
console.log("There is an error inside your worker!");
};
The same snippet, but using addEventListener():
js
self.addEventListener("error", () => {
console.log("There is an error inside your worker!");
});
Specifications
| Specification |
|---|
| HTML> # handler-workerglobalscope-onerror> |
Browser compatibility
See also
The WorkerGlobalScope interface it belongs to.