Node: [worker_threads] make JS new Error() copyable across threads

Created on 15 Mar 2019  路  3Comments  路  Source: nodejs/node

node version: v11.11.0

Today I have to normalize errors to send worker thread errors back to main thread:

parentPort.postMessage({ error: normalizeError(error) })

function normalizeError(errorObject) {
  return Object.keys(errorObject)
    .reduce((result, key) => Object.assign(result, { [key] : errorObject[key] }), {});
}

worker

Most helpful comment

Hi!

One issue with this is that we鈥檙e following the HTML spec for structured cloning as far as message passing is concerned. Web specs generally don鈥檛 do breaking changes, and I think this would be a breaking change, although I鈥檇 personally love to see something like this.

(Fwiw, we have a rather complex implementation of error serialization for this reason as well.)

/cc @nodejs/workers

All 3 comments

Hi!

One issue with this is that we鈥檙e following the HTML spec for structured cloning as far as message passing is concerned. Web specs generally don鈥檛 do breaking changes, and I think this would be a breaking change, although I鈥檇 personally love to see something like this.

(Fwiw, we have a rather complex implementation of error serialization for this reason as well.)

/cc @nodejs/workers

Updating this thread for posterity / paper trail.

There is movement on the spec / browser side for this. Since @addaleax's comment above the MDN article now calls out that Chrome does clone native Error objects, Firefox is working to implement, Safari has a bug open here, and the whatwg is tweaking spec semantics slightly to support.

Okay, I think that this can be closed then :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seishun picture seishun  路  3Comments

mcollina picture mcollina  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments

ksushilmaurya picture ksushilmaurya  路  3Comments

fanjunzhi picture fanjunzhi  路  3Comments