Suggestion: error reporting API

I would like a way to report an error explicitly so it gets enqueued for window.onerror and friends.

Currently, I’ve been resorting to this, though I’ve not historically gotten consistent results even in the REPL as it relies on garbage collection to work and I’ve had browsers recognize the synchronous aspect before:

// What I currently use
function reportError(e) {
  new Promise((_, reject) => { reject(e) })

// What I previously used to work around a V8
// unhandled rejection reporting bug
function reportError(e) {
  Promise.resolve().then(() => { throw e })

// If rethrowing changes the stack and the first
// variant fails to generate a report, you'll
// need to blend the two
function reportError(e) {
  Promise.resolve().then(() =>
    new Promise((_, reject) => { reject(e) })

Note: I’ve also run into issues with the second variant in the past as well due to various browsers like Chrome and WebKit (especially in old PhantomJS) replacing stacks.

A method that doesn’t have that element of non-determinism and implementation detail reliance would be far superior to that, and it’d also ease integration with the reporting API proposal as well as I’d only have one mechanism I’d have to integrate with.

Note: the usual mechanism of console.error is not acceptable, as it doesn’t actually trigger an error event.

^^ @Ian_Clelland

This looks similar to the reportError method that we’d like to ship in Chrome (and is already in HTML) – that should fire window.onerror.

Re: integrating with the Reporting API; we’ve discussed previously the idea of allowing script-generated reports, but never reached agreement that it was a good idea (outside of tests). That’s not a final decision by any means, though – this issue would be a good place to discuss that aspect.

Fantastic! Commented: