Skip to content
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

mongoose dosen't throw error when network disconnected #12063

Closed
2 tasks done
uuuuuup opened this issue Jul 7, 2022 · 3 comments · Fixed by #12110
Closed
2 tasks done

mongoose dosen't throw error when network disconnected #12063

uuuuuup opened this issue Jul 7, 2022 · 3 comments · Fixed by #12110
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@uuuuuup
Copy link

uuuuuup commented Jul 7, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.4.0

Node.js version

16.13.1

MongoDB server version

5.0.7

Description

Mongoose dosen't throw error when network is disconnected.
When I call mongoose find function, I can not get error.
So server is stuck forever and can not send response to user.

When I call mongodb find function with the mongoose connection, and I can get below error.
MongoServerSelectionError: connect ECONNREFUSED x,x,x,x:27017
at Timeout._onTimeout

I use nest.js framework for server.

Steps to Reproduce

  1. start mongo server
  2. start nest.js server
  3. nest.js server connect to mongo server
  4. stop mongo server
  5. I send GET command to nest.js server
  6. nest.js server execute find function
  7. wait timeout

Expected Behavior

Mongoose should throw error.

@vkarpov15
Copy link
Collaborator

Mongoose will throw a MongooseServerSelectionError after serverSelectionTimeoutMS. serverSelectionTimeoutMS is 30000 (30 seconds) by default, which is why it seems like the server is stuck, but it actually isn't.

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Jul 11, 2022
@uuuuuup
Copy link
Author

uuuuuup commented Jul 12, 2022

@vkarpov15

OK, I got error as below.
TypeError: Invalid URL
at new NodeError (node:internal/errors:372:5)
at URL.onParseError (node:internal/url:563:9)
at new URL (node:internal/url:643:5)
at isAtlas (/node_modules/mongoose/lib/helpers/topology/isAtlas.js:17:17)
at MongooseServerSelectionError.assimilateError (/node_modules/mongoose/lib/error/serverSelection.js:35:35)

It can be caught as below.
process.on('uncaughtException', () => {})

But I could not catch with below code.
try {
call.save();
} catch (err) {
console.log(err);
}
Can I catch the error with try / catch?

When I use mongodb api not mongoose api, I could catch the error with try / catch.

skrtheboss added a commit to skrtheboss/mongoose that referenced this issue Jul 15, 2022
Use ServerDescription methods for getting server host and port instead of parsing hostname,
which did not work properly.

Closes Automattic#12063
skrtheboss added a commit to skrtheboss/mongoose that referenced this issue Jul 15, 2022
Use ServerDescription methods for getting server host and port instead of parsing hostname,
which did not work properly.

Closes Automattic#12063
@vkarpov15
Copy link
Collaborator

try { await call.save(); } catch (err) { ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants