josdejong / workerpool

Offload tasks to a pool of workers on node.js and in the browser
Apache License 2.0
2.06k stars 147 forks source link

Type of error is not kept when error goes from inside to outside the workerpool #363

Closed slimee closed 10 months ago

slimee commented 1 year ago

Figure an error of "type" CalculationError

class CalculationError extends Error{};

And it's loaded and thrown from inside the workerpool:

throw new CalculationError();

The error propagates from inside the workerpool context to the caller context.

The statement error instanceof CalculationError is:

Do you notice this behavior? How should I preserve the type of errors thrown through the workerpool context? Regards

josdejong commented 1 year ago

The data transferred between the main process and a worker must be plain JSON. Any class instance information is lost. If you want to transfer a class instance, you'll need to put a serialization and deserialization logic of your own in place. If you want to transfer an error class, maybe you can let your function return an object { success, error } instead of throwing an exception, in combination with logic to turn your data and error message into plain JSON.