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.