-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Unhandled promise rejections are confusing to new users #16768
Comments
We could add a URL to a web page explaining the problem in more detail, some other frameworks do something similar when an internal error occurs. |
Maybe we should code the warnings and document them all in a central place, like what we do for errors? |
List of all [Ee]mitWarning I've found https://gist.github.com/refack/4fbb1e7411d10c82f6a18bd9556f70ab |
Looking at the current mechanism, seems like it should be easy to restructure the warning's message:
|
#16768 Another one |
@refack thanks: Expanding on your suggestion, maybe something like:
Here is a bike chain: (node:52553) Unhandled Async Error: ReferenceError: user is not defined
at Foo (foo.js:1:15)
at Bar (bar.js:10:15)
(node:52553) This error originated in an `async` function throwing or a rejected promise which was not handled in the code.
(node:52553) [DEP0018] DeprecationWarning: Unhandled promise rejections are dangerous. In the future, promise rejections that are not handled might terminate the Node.js process with a non-zero exit code. WDYT @refack ? Also looking for feedback from @jasnell who worked on the original warning API, @BridgeAR and @Fishrock123 for working on the "exit on GC" stuff and @addaleax for promise work and knowing these APIs very well. |
maybe loop in @mcollina too fwiw my preference is: "exit process in nextTick if no event handler is registered" |
The problem is that that are some people who are very strong opponents to that suggestion but won't voice their concern here. Personally I'm really in favor of "warn after nextTick, exit on GC, exit after nextTick warning if wasn't handled for N seconds where N is large". That said, this issue is more about the current warning message being confusing. |
My overall point on why any Node.js server should exits on unhandledRejection is listed in https://github.com/mcollina/make-promises-safe. The same very likely holds true for CLI tools. Both a) not printing the error stack trace and b) not exiting are definitely hindering the ability to use async/await and promises reliably in Node applications. when we exit is less of a constraint: I prefer exiting on nextTick, but exiting on gc is also ok. Consensus has eluded us on this topic for a long time, can we target this for 10.0.0? |
I don't think exiting on nextTick is viable (although it's what I personally do often), consider: async function foo(user) {
var expensive = fetchUserInfoFromDb(user);
var group = await fetchUserSmallInfoFromCache(user);
// do some work on `group`
// now we need the rest
let userInfo = await expensive;
await somethingOnBoth(userInfo, group);
} I have to be very careful to not write code like this and I suspect it's a footgun users might run into. Ideally I'd like promises inside an |
@benjamingr I found that code is lacking intent of not caring about errors for a short span: async function foo(user) {
var expensive = fetchUserInfoFromDb(user);
// we don't care about the error until we await
expensive.catch(noop);
var group = await fetchUserSmallInfoFromCache(user);
// do some work on `group`
// now we need the rest
let userInfo = await expensive;
await somethingOnBoth(userInfo, group);
}
function noop() {} It's a tradeoff. On a side note, our current approach (warn) will actually result in a false positive warning in the case above. Not nice. An incremental step for Node 10 is to start printing the full error message for every |
As for the warning - I like @benjamingr's suggested format as above. |
I'm familiar with an empty I don't think it's reasonable to expect users to do Both approaches (GC and nextTick) have issues:
Which is why I think we should warn (with a friendlier warning and a trace) on nextTick and exit the process on GC - so we exit safely and warn safely. |
On it. #goodnessSquad |
In my case, the reason was app-preferences plugin in Ionic 3 project. (node:21236) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Unexpected end And couldn't remove plugin by
under installed_plugins.
Note that, the app-preferences plugin isn't working properly with cordova 8.0.0. |
D:\ANGUALRJS2 PROJECTS\PR02>ngh (node:4020) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Unspecified error (run without silent option for detail) |
I am also with stuck with this error during build. I am using node v10.0.0 |
Did you not get a stack trace with that? Is that the full error @diatoz @subhashekkaluru ? |
I also have the same issue. Nothing on the stack trace.
|
@benjamingr running with thanks for the explanation |
@lucasdellabella thanks for commenting :) Could always use more people interested in improving the debugging experience in Node.js. We've made the stack trace always show (even without the --trace-warnings flag) in Node.js 10 - so a possible solution would be to update your Node.js version. The V8 team are working on giving us async stack traces in production - so we'll get much better errors soon too :) |
This change adds more debugging context to any unhandled promise. If the unhandled promise does not have a reason which is an error, it is very difficult to determine where the error came from. This ensures every reason has an appropriate stack trace before the "next_tick" when the stack is lost. Refs: nodejs#16768
I have this error in pm2 logs mm `
` |
Hopefully this new feature doesn't cause promise rejections to kill a REPL session. I sometimes do things like this:
....and I see the UnhandledPromiseRejectionWarning immediately. I shouldn't have to type in a |
Is there any flag to make this deprecation a reality ? I would like to avoid having unhandled promises not returning a non 0 exit code. |
@L1br3 |
@tniessen well tried :-) but gave me this error: EDIT: working correctly on node v12.x |
@L1br3 you are running an old version of Node |
@benjamingr Well spotted ! That was the issue, the flag is working correctly. Thanks ! |
I try to monitor and answer Node.js promise questions in StackOverflow. Several times now this sort of question arises where the OP gets an unhandled rejection error:
Which I then explain is just a regular error wrapped in an
UnhandledPromiseRejectionWarning
.I think changing the error format could really help the user understand the problem. Currently the average async/await user has to:
--trace-warnings
in order to get a reasonable stack trace for said error.I think the current error format is pretty unwelcoming to new users and while it is a huge improvement over swallowing the error by default or not having a hook into it - we should improve it.
This issue is about the phrasing of the current warning and does not interfere with ongoing efforts to throw on unhandled rejections when they're detected in GC.
Does anyone have any better naming suggestions?
(Note, I'd prefer (if possible) that we keep using the existing warnings infrastructure for the warning)
The text was updated successfully, but these errors were encountered: