Search code examples
javascriptnode.jsmultithreadingerror-handlingweb-worker

How do I get errors to the console from node-webworker-threads?


I am developing a node.js application. I am using the webworker-threads package to spin off some CPU blocking tasks. However, uncaught errors in my thread code do not get sent to the console. Instead, the thread just fails silently. The simplest case illustrates this:

var Threads = require('webworker-threads');
this.testThread = new Threads.Worker( function() {
      console.log("This is the test thread. The next line should throw an error.");
      undefinedFunction();
    } );

Since my actual application is calling an separate js file as the thread, I am also not seeing any syntax errors reported. Is there some undocumented error event I should be catching? Is there perhaps something wrong with my environment?


Solution

  • Try binding to the worker's onerror event:

    var webpackWorker = new Worker(function() {             
        this.onmessage = function(event) {
           throw ('test');
        };
    });
    
    webpackWorker.onerror = function (event) {
        console.log(event);
    }
    
    webpackWorker.postMessage("dummy");