Keep track of the context in which an exception was thrown#95
Keep track of the context in which an exception was thrown#95andreubotella wants to merge 1 commit into
Conversation
This is needed for `error` events in the web integration (equivalent
to Node.js's `process.on("uncaughtException")`).
This might be used by web specs like this:
> 1. Let `previousContext` be AsyncContextSwap(`someContext`).
> 2. Invoke `someCallback`. If this throws an exception `err`, then:
> 1. Let `throwContext` be the surrounding agent's
> [[ThrowAsyncContextMapping]].
> 2. If `throwContext` is EMPTY, set `throwContext` to
> AsyncContextSnapshot().
> 3. Report the exception `err` with `throwContext`.
|
After some thought, I'm not sure that having the asyncVar.run("foo", () => {
setTimeout(() => {
asyncVar.run("bar", () => {
throw new Error();
});
}, 1000);
});
window.onerror = () => {
console.log(asyncVar.get());
};In the above code, the current spec with the proposed web integration would log "foo", but with this PR (and the proposed HTML spec changes) it would log "bar". The reason I'm not sure this would be useful is that we see two main use cases for this proposal: tracing, and tracking provenance from concurrent isolated applications (see #107 for more on this distinction). For tracing, according to @legendecas there's not a clear benefit for this PR, and in the case of tracking isolated applications, you would rarely have a case where this would make a difference. However, one case where there would be a difference for isolated applications is if an error happens during or synchronously after the initialization of such an application: // The context here is the empty context.
for (let i = 0; i < 10; i++) {
setTimeout(() => {
asyncVar.set(i, async () => {
if (Math.random() > 0.5) {
// Throwing here would fire an error event with the empty context.
throw new Error();
}
await doSomething();
if (Math.random() > 0.5) {
// Throwing here would fire an unhandledrejection event with the correct context.
throw new Error();
}
});
}, 0);
}How relevant is this for that use case? |
In this example, the anonymous function passed to |
This is needed for
errorevents in the web integration (equivalent to Node.js'sprocess.on("uncaughtException")).The exact spec infrastructure for this is not yet settled, this is only one proposal.
This might be used by web specs like this: