Here, error handling is a bit tricky. Since the error can occur anywhere in the script by any port (any parent file), you have to manually send the error to every port. But for that, you'll have to store the ports, as well (when they're connected). Here's how it should look:
// myworker.jsconst ports = [];addEventListener('connect', e => { const port = e.ports[0]; ports.push(port); // assemble all connections port.start(); // .. other info});addEventListener('error', e => { console.log(e); // Info about error ports.forEach(port => port.postMessage({type: 'error', res: e}));});
Here you can see that we are manually sending the error information to every parent file. Thereafter, you can handle the error in the parent file itself. ...