wintercg / proposal-common-minimum-api

https://common-min-api.proposal.wintercg.org/
Other
216 stars 15 forks source link

Consider adding `globalThis.reportError()` #41

Open andreubotella opened 1 year ago

andreubotella commented 1 year ago

The HTML spec defines a reportError function which takes a JS value (usually an exception object) and acts as if that value had been thrown, without actually stopping the execution of the current function and its callstack. This seems fairly useful in server-side environments.

Acting "as if the value had been thrown" in browsers includes firing an error event at the global, propagating up the chain of workers if called inside a dedicated worker, and eventually, if the error is not handled at any step along the way, logging the corresponding error message on the console.

However, specifying this for server-side runtimes would involve having to deal with the runtime's existing error handling mechanisms, which might be quite different to those in browsers. (See also #29)

ljharb commented 1 year ago

See https://github.com/nodejs/node/issues/38947 / https://github.com/nodejs/node/pull/41912

andreubotella commented 1 year ago

Also https://github.com/denoland/deno/issues/13484#issuecomment-1054908091 for the discussion about adding it to Deno, which did end up happening.

Note that patterns such as setTimeout(() => {throw err;}, 1) are used on Node.js code, so it's not like this API wouldn't find any use.

On that note, the "report an error" spec algorithm is used by a number of the APIs in the list, and we should probably mention that runtimes which claim to be WinterCG-compatible can override the behavior of this algorithm to exit instead.

jasnell commented 3 months ago

PR to add reportError(...) to workers https://github.com/cloudflare/workerd/pull/1979