Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

fix(server): emit "first connect" error if initial connect fails due to ECONNREFUSED #438

Merged
merged 1 commit into from
May 28, 2019

Conversation

vkarpov15
Copy link
Member

Admittedly this is a minor detail, but when connecting to a standalone server that is currently down using the below script:

const mongodb = require('mongodb');

const connectionString = `mongodb://localhost:27017/test`;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongodb.MongoClient.connect(connectionString, { useNewUrlParser: true, connectTimeoutMS: 5000 });
}

You get the below error message:

Error: connect ECONNREFUSED 127.0.0.1:27017
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)

This error gets bubbled up because for some reason the pool hasn't transitioned its state from 'CONNECTING' to 'DISCONNECTED' when the code that's supposed to format the error runs. With this change, you instead get the below error:

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (/home/val/Workspace/MongoDB/mongoose/node_modules/mongodb-core/lib/topologies/server.js:432:11)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at connect (/home/val/Workspace/MongoDB/mongoose/node_modules/mongodb-core/lib/connection/pool.js:557:14)
    at makeConnection (/home/val/Workspace/MongoDB/mongoose/node_modules/mongodb-core/lib/connection/connect.js:39:11)
    at callback (/home/val/Workspace/MongoDB/mongoose/node_modules/mongodb-core/lib/connection/connect.js:261:5)
    at Socket.err (/home/val/Workspace/MongoDB/mongoose/node_modules/mongodb-core/lib/connection/connect.js:286:7)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Re: Automattic/mongoose#7768

@vkarpov15 vkarpov15 requested a review from mbroadst May 27, 2019 18:27
@mbroadst mbroadst requested a review from daprahamian May 28, 2019 18:57
@mbroadst mbroadst merged commit 853bcfe into master May 28, 2019
@mbroadst mbroadst deleted the vkarpov15-patch-1 branch May 28, 2019 20:36
@mbroadst
Copy link
Member

Thanks Val! So for the next minor release we've merged core and native, and we don't have a great system for cherry-picking fixes across. I can apply the fix there, but we will lose attribution. Would you be interested in submitting a PR against node-mongodb-native#next with this fix as well?

@mbroadst
Copy link
Member

mbroadst commented Jun 7, 2019

@vkarpov15 ping on the last comment there. We can take care of it for you, just wanted to make sure you were attributed

@vkarpov15
Copy link
Member Author

Thanks for your patience and the reminder Matt, I'd forgotten about this. I put in a PR against the next branch ☝️

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants