-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unhandledrejection is not working for anonymous async function promises. Some code works in nodejs #16928
Comments
This works perfectly fine for me on Deno 1.28.3:
Handling of |
@bartlomieju 1.18.3 was a typo, I mean 1.28.3 I checked it and you are right. It works when running from empty directory. But it does not with my deno.lock file. I created a repository which reproduces it.
|
Even if I refresh the lock file, it still doesn't work. |
Interesting, I can indeed reproduce it with a lock file. |
Digged a bit more and this is indeed coming from the Node compatibility layer, see: globalThis.addEventListener("unhandledrejection", (event) => {
if (process.listenerCount("unhandledRejection") === 0) {
// The Node.js default behavior is to raise an uncaught exception if
// an unhandled rejection occurs and there are no unhandledRejection
// listeners.
if (process.listenerCount("uncaughtException") === 0) {
throw event.reason;
}
event.preventDefault();
uncaughtExceptionHandler(event.reason, "unhandledRejection");
return;
}
event.preventDefault();
process.emit("unhandledRejection", event.reason, event.promise);
}); We're hitting the:
branch which causes the exception to be raised. I'm not sure what would be the best course of action here, @dsherret @cjihrig thoughts? |
Related #16943 |
@bartlomieju Do you have any idea how we could fix this? |
We've discussed this and still debating how to do it without having to introduce a special API for Node compat layer, however we don't have any good ideas, so we might have to add a special API. This is still on my radar, but I had to switch to work on the release. I'll try to get it fixed before end of the year. |
How about adding a special API to get listener count of EventTarget? const listenerCount = Symbol("listenerCount"); // only available to Deno internals
class EventTarget {
...
/** Gets the count of listeners */
[listenerCount](type: string)]: number {...}
} And use it like: if (process.listenerCount("uncaughtException") === 0 && globalThis[listenerCount]("unhandledrejection") === 0) {
throw event.reason;
} |
Here's an idea (JK!): import { delay } from "https://deno.land/[email protected]/async/delay.ts";
globalThis.addEventListener("unhandledrejection", (e) => {
console.error("unhandledRejection");
e.preventDefault();
});
window.addEventListener("error", (ev) => {
console.error("snatched error:", ev.error);
ev.preventDefault();
});
(async () => {
throw "failed";
})();
await delay(2_000);
console.log("success"); output:
|
@piscisaureus both these event listeners are already installed by |
That might also work. Do you suggest special event registration API like |
I was suggesting a private API like: |
Note to self: |
This should be much more approachable now with #17724 landed |
…mports (#19235) This commit fixes emitting "unhandledrejection" event when there are "node:" or "npm:" imports. Before this commit the Node "unhandledRejection" event was emitted using a regular listener for Web "unhandledrejection" event. This listener was installed before any user listener had a chance to be installed which effectively prevent emitting "unhandledrejection" events to user code. Closes #16928
Deno (1.18.3)
Node (19.1.0)
Result:
error: Uncaught "failed"
Expected:
success
The text was updated successfully, but these errors were encountered: