thread-stream icon indicating copy to clipboard operation
thread-stream copied to clipboard

Custom error objects aren't cloneable

Open jasnell opened this issue 3 years ago • 4 comments

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.

jasnell avatar Jul 21 '21 14:07 jasnell

Is this actionable for this module? How could we improve things?

mcollina avatar Jul 21 '21 16:07 mcollina

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.

jasnell avatar Jul 21 '21 19:07 jasnell

I would not think so, anyway let's keep this open and we'll see if it becomes a problem.

mcollina avatar Jul 22 '21 12:07 mcollina

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) ? )

davidmarkclements avatar Jul 22 '21 15:07 davidmarkclements