thread-stream
thread-stream copied to clipboard
Custom error objects aren't cloneable
Just something to remember... in https://github.com/pinojs/thread-stream/blob/main/lib/worker.js, when the destination error event is emitted, an 'ERROR' message is posted back to the origin thread...
https://github.com/pinojs/thread-stream/blob/main/lib/worker.js#L21-L26:
destination.on('error', function (err) {
parentPort.postMessage({
code: 'ERROR',
err
})
})
Error
objects can be a bit odd in the structured clone algorithm. If err
is a custom error object (e.g. something like Node.js DOMException
or something like class MyError extends Error
, what comes out on the other side will not match exactly. It will certainly clone as an Error
, but you'll lose the custom extension on it.
Is this actionable for this module? How could we improve things?
It's likely more something just to document just in case. If you're not using custom errors at all then things should Just Work, but if any part of this is relying on things like a DOMException
thrown from Node.js the results aren't going to be what you expect.
I would not think so, anyway let's keep this open and we'll see if it becomes a problem.
you could provide an API for registering custom error constructors in the worker thread, then you'd have to somehow communicate that an error object that has entered the worker thread should be "recast" (Object.setPrototypeOf(CustomError.call(err), CustomError.prototype) ? )