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
How to catch MongoNetworkError: connect ECONNREFUSED in Node when using mongoose.createConnection() #14455
Comments
Try adding |
No, also asPromise() the mongoose.createConnection the catch doesn't get called.
|
Once initial connection is completed, Given that you aren't properly handling errors in your original post (missing try {
TestModel.findOne();
} catch (err) {
handleError(err);
}
try {
await TestModel.findOne();
} catch (err) {
handleError(err);
} |
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
This issue was closed because it has been inactive for 19 days and has been marked as stale. |
This might help:
|
Prerequisites
Mongoose version
8.0.0
Node.js version
21
MongoDB server version
5.0
Typescript version (if applicable)
No response
Description
on a lost connection there is no clear way to trap MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
thrown from: node_modules/mongodb/lib/sdam/topology.js:276:34
The connection is created as
Steps to Reproduce
The following appears:
MongoNetworkError: connection 1 to 192.168.32.2:27017 closed
at Connection.onClose (/opt/backend/node_modules/mongodb/lib/cmap/connection.js:124:28)
at Socket. (/opt/backend/node_modules/mongodb/lib/cmap/connection.js:66:46)
at Socket.emit (node:events:519:28)
at TCP. (node:net:337:12) {
[Symbol(errorLabels)]: Set(0) {},
[cause]: undefined
After 2 more seconds the disconnect callback is triggered and the following appear:
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
backend | 13:31:20 0|main | at EventTarget. (/opt/backend/node_modules/mongodb/lib/sdam/topology.js:276:34)
backend | 13:31:20 0|main | at [nodejs.internal.kHybridDispatch] (node:internal/event_target:822:20)
backend | 13:31:20 0|main | at EventTarget.dispatchEvent (node:internal/event_target:757:26)
backend | 13:31:20 0|main | at abortSignal (node:internal/abort_controller:374:10)
backend | 13:31:20 0|main | at TimeoutController.abort (node:internal/abort_controller:396:5)
backend | 13:31:20 0|main | at Timeout. (/opt/backend/node_modules/mongodb/lib/utils.js:1011:92)
backend | 13:31:20 0|main | at listOnTimeout (node:internal/timers:573:17)
backend | 13:31:20 0|main | at process.processTimers (node:internal/timers:514:7) {
backend | 13:31:20 0|main | reason: TopologyDescription {
backend | 13:31:20 0|main | type: 'Unknown',
backend | 13:31:20 0|main | servers: Map(1) { 'mongo1:27017' => [ServerDescription] },
backend | 13:31:20 0|main | stale: false,
backend | 13:31:20 0|main | compatible: true,
backend | 13:31:20 0|main | heartbeatFrequencyMS: 10000,
backend | 13:31:20 0|main | localThresholdMS: 15,
backend | 13:31:20 0|main | setName: null,
backend | 13:31:20 0|main | maxElectionId: null,
backend | 13:31:20 0|main | maxSetVersion: null,
backend | 13:31:20 0|main | commonWireVersion: 0,
backend | 13:31:20 0|main | logicalSessionTimeoutMinutes: null
backend | 13:31:20 0|main | },
backend | 13:31:20 0|main | code: undefined,
backend | 13:31:20 0|main | [Symbol(errorLabels)]: Set(0) {},
backend | 13:31:20 0|main | [cause]: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
backend | 13:31:20 0|main | at connectionFailureError (/opt/backend/node_modules/mongodb/lib/cmap/connect.js:379:20)
backend | 13:31:20 0|main | at Socket. (/opt/backend/node_modules/mongodb/lib/cmap/connect.js:285:22)
at Object.onceWrapper (node:events:634:26)
at Socket.emit (node:events:519:28)
at emitErrorNT (node:internal/streams/destroy:169:8)
at emitErrorCloseNT (node:internal/streams/destroy:128:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
[Symbol(errorLabels)]: Set(1) { 'ResetPool' },
[cause]: Error: connect ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1605:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 27017
}
}
}
Expected Behavior
try{
mongooseConnection = mongoose.createConnection(mongoUri, {
serverSelectionTimeoutMS: 5000
});
....
}
catch(e){
console.error(e);
}
The Error should have been reported in the catch block
The text was updated successfully, but these errors were encountered: