Adapter Method Promise Rejections are Unhandled, causes Unreliable Upstream Promise Resolutions
See original GitHub issueDescribe the bug
Promises that reject with errors from within adapter methods are in some cases unhandled, leading to UnhandledPromiseRejectionWarning
and potentially leaving pending upstream promises that never resolve.
An example of such an unhandled promise rejection is during the email sign-in flow. In this case, if the adapter method getUserByEmail
rejects with an error, then an UnhandledPromiseRejectionWarning
is logged to the node
console.
In development, upstream promises (such as the one returned by the client signIn
method when redirect
is false
) that rely on the getUserByEmail
promise resolving or rejecting are left indefinitely pending.
In production, upstream promises are eventually rejected by virtue of a 500
response from the server as it deals with the unhandled rejection exception.
Steps to reproduce Either clone repro: https://github.com/WillTheVideoMan/nextauth-repro-unhandled-promise-rejection-warning
Or:
- Configure
next-auth
in a development environment with an adapter. - To test this case, configure the adapter such that the adapter methods always reject with an error e.g. provide incorrect database credentials.
- Make a request to an endpoint which contains an unhandled adapter promise e.g. https://github.com/nextauthjs/next-auth/blob/17b789822de64eb845e1e8e49ea83dbff56344f4/src/server/routes/signin.js#L42
- Observe an
UnhandledPromiseRejectionWarning
warning in thenode
console. - Observe that any upstream promises such as the one making the request are indefinitely pending.
- Deploy instance to a production environment.
- Observe that the upstream promises are resolved eventually by virtue of a
500
response.
Expected behavior Expect all promises which may reject to be handled explicitly.
I think the overall intention here should be to avoid unhandled promise rejections entirely. This would ensure errors more consistently cascade in predictable ways, and importantly, would move in line with the upcoming node v15
revisions: https://github.com/nodejs/node/pull/33021
Screenshots or error logs
(node:21664) UnhandledPromiseRejectionWarning: Error: get_user_by_email_error
at getUserByEmail (webpack-internal:///./auth/fauna-adapter.js:135:31)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:21664) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 14)
Additional context Whilst ultimately the production environment is the one to worry about, enabling consistent error handling throughout NextAuth would reduce the disparity between development and production. I discovered this issue exactly because of the lack of promise fulfilment in development, and so I had no way to properly test my implementation of error handling. This is a make-or-break for our testing methodology. It should be that we get the same fundamental behavior in development as we do in production, independent of any intrinsic NextJS magic.
Feedback
- Found the documentation helpful
- Found documentation but was incomplete
- Could not find relevant documentation
- Found the example project helpful
- Did not find the example project helpful
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:21 (13 by maintainers)
This is now in
next-auth@3.20.0
! @WillTheVideoMan do you want to have a look at anything else before this issue is closed?@balazsorban44 @kripod This is so great! I will pull down the latest canary release and see how things are working out.
I am incredibly grateful that you both are receptive and proactive to these kinds of issues, where elsewhere they may go unnoticed for months, or stall whilst awaiting some corporate approval.
This is a true open-source mindset, and can only be positive for the package and the NextJS ecosystem as a whole.
Thank you for your time and energy. It is greatly valued.