The error event of the Worker interface fires when an error occurs in the worker. Learn onerror vs addEventListener("error"), how it differs from the message event, and how to recover—with five examples and try-it labs.
01
Kind
Instance event
02
Name
error
03
Type
Event (ErrorEvent)
04
Handler
onerror
05
vs
message
06
Status
Baseline widely
Fundamentals
Introduction
Workers run on another thread. If their script throws, the main page does not get a normal JavaScript exception in your postMessage call. Instead, the Worker object fires an error event.
Attach a handler early—before work starts—so you can show a friendly message, log details, and optionally terminate() a broken worker.
💡
Beginner tip
Successful replies use the message event (event.data). Failures use the error event. Listen for both when you depend on a worker.
Concept
Understanding the Worker error Event
An instance event on Worker that fires when an error occurs in the worker.
A generic Event (per MDN). In practice you may also read ErrorEvent properties such as message.
Typical pattern
JavaScript
const myWorker = new Worker("worker.js");
myWorker.onerror = (event) => {
console.log("There is an error with your worker!");
console.log(event.message); // often available
};
Compare
⚖️ error vs Related Ideas
Idea
Meaning
error event
Worker script failed
message event
Successful postMessage data arrived
worker.onerror
Handler property for error
addEventListener("error")
Same event; can stack multiple listeners
terminate()
Hard-stop a worker after a fatal error
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Handler property
worker.onerror = (e) => { … }
addEventListener
worker.addEventListener("error", handler)
Read message
event.message (when ErrorEvent)
Recover
Log, notify UI, optionally terminate()
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about the Worker error event.
Fires
on failure
In the worker
Listen
onerror
Or addEventListener
≠ message
different
Not event.data
Baseline
widely
Since Jul 2015
Hands-On
Examples Gallery
Examples follow MDN Worker: error event. Labs use blob workers that throw so you can see the handler fire.
📚 Getting Started
Attach handlers the two standard ways.
Example 1 — onerror Handler (MDN Idea)
Set onerror on the Worker object.
JavaScript
const myWorker = new Worker(url);
myWorker.onerror = (event) => {
console.log("There is an error with your worker!");
event.preventDefault();
};
The Worker error event is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. It is available in Web Workers except Service Workers.
✓ Baseline · Widely available
Worker error event
Fires when an error occurs in the worker. Listen with onerror or addEventListener("error").
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported with workers in IE10+ (prefer modern browsers)
Legacy
Worker errorExcellent
Bottom line: Always attach an error handler when you depend on a dedicated worker—success uses message; failure uses error.
Wrap Up
Conclusion
The Worker error event is how the main thread learns that a dedicated worker failed. Pair it with message for success, and recover with logging, UI updates, and terminate() when needed.
Assume try/catch around postMessage catches worker throws
Confuse app-level error messages with the error event
Leave the UI waiting after an error with no timeout
Overwrite onerror accidentally in shared helpers
Ignore load failures for missing worker scripts
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Worker error
Main-thread signal that the worker failed.
5
Core concepts
⚠️01
Fires
on worker fail
API
📌02
onerror
or listener
Listen
💬03
≠ message
separate channel
Compare
🛑04
Recover
terminate / restart
Fix
🎯05
Baseline
since Jul 2015
Status
❓ Frequently Asked Questions
It fires on the Worker object when an error occurs in the worker script. Listen with worker.onerror or worker.addEventListener("error", handler).
No. MDN marks the Worker error event as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is available in Web Workers except Service Workers.
MDN documents it as a generic Event. In browsers you often receive an ErrorEvent with helpful fields such as message (and sometimes filename, lineno, colno).
message delivers successful postMessage data as event.data. error reports that the worker script failed. Do not confuse an application-level { type: "error" } message with the real error event.
Yes for production workers. Without a handler, worker failures can be easy to miss while the UI waits forever for a reply.
Some browsers let you call event.preventDefault() in the error handler to suppress the default error reporting UI/console behavior. Always still log and recover in your own code.
Did you know?
A try/catch around worker.postMessage(data) only catches problems sending the message on the main thread. Errors that happen later inside the worker arrive through this error event instead.