Example 1 — onerror (MDN)
Attach onerror when the worker script throws.
const code = "throw new Error('boom');";
const url = URL.createObjectURL(
new Blob([code], { type: "application/javascript" })
);
const myWorker = new Worker(url);
myWorker.onerror = (event) => {
console.log("There is an error with your worker!");
event.preventDefault();
myWorker.terminate();
URL.revokeObjectURL(url);
}; How It Works
- Blob worker throws. Uncaught error inside the worker.
onerrorruns on main. MDN’s handler pattern.- Log a clear message. User-facing or debug text.
- Clean up. Terminate and revoke the blob URL.

