question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adapter Method Promise Rejections are Unhandled, causes Unreliable Upstream Promise Resolutions

See original GitHub issue

Describe 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:

  1. Configure next-auth in a development environment with an adapter.
  2. To test this case, configure the adapter such that the adapter methods always reject with an error e.g. provide incorrect database credentials.
  3. 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
  4. Observe an UnhandledPromiseRejectionWarning warning in the node console.
  5. Observe that any upstream promises such as the one making the request are indefinitely pending.
  6. Deploy instance to a production environment.
  7. 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
balazsorban44commented, May 5, 2021

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?

3reactions
WillTheVideoMancommented, May 3, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add missing error explanations to docs · Issue #1089 · nextauthjs ...
balazsorban44 mentioned this issue on Apr 23, 2021. Adapter Method Promise Rejections are Unhandled, causes Unreliable Upstream Promise Resolutions #1832.
Read more >
What is an unhandled promise rejection? - Stack Overflow
A rejected promise is like an exception that bubbles up towards the application entry point and causes the root error handler to produce...
Read more >
Handling those unhandled promise rejections with JS async ...
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch ... The promise rejected with the reason “…...
Read more >
CHANGELOG.md - Google Git
Fixed unhandled promise rejection error in debug adapter (Issue 982). Enhancements. Disabled the experimental godlvdap debug configuration from the stable ...
Read more >
The Difference Between Node.js 10 LTS and Node.js 12 LTS
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found