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

Catch connection errors #24

Closed
rauchg opened this issue Nov 2, 2012 · 6 comments · Fixed by #142
Closed

Catch connection errors #24

rauchg opened this issue Nov 2, 2012 · 6 comments · Fixed by #142
Labels

Comments

@rauchg
Copy link
Contributor

rauchg commented Nov 2, 2012

Right now errors from mongoskin/native emitters are not being catched.
They should be re-emitted so that they can be captured by manager.on('error')

@matthewmueller
Copy link

+1, trying to get some kind of feedback that the connection hasn't been established.

@rauchg
Copy link
Contributor Author

rauchg commented Jan 23, 2013

Yep basically we need to re-emit as error. Also, if an error occurs before open was emitted, we can fire it as connection_error as well.

@rauchg
Copy link
Contributor Author

rauchg commented Jan 23, 2013

I also wonder whether we have open and close haha

@matthewmueller
Copy link

haha, i was actually getting open events even when i didn't have mongod running. That seemed a bit off, but I wasn't sure.

Here's what i had:

monk = require('monk')(url);

monk.on('open', function() {
  // prob shouldn't be firing when mongod not running...
});

@carloscarcamo
Copy link

+1

@carloscarcamo
Copy link

Maybe the bug is on manager.js in:

 this.driver.open(this.onOpen.bind(this));

What if we just pass a callback and emit events?, something like:

var _this = this;
this.driver.open(function (error) {
  if (error) {
    _this.emit('error', error);
  } else {
    _this.emit('open')
  }
});

...

if (fn) {
  this.once('open', fn);
  this.once('error', fn);
}

Then we can pass a callback to check for errors, something like:

var db = monk(uri, function(err) {
  if (err) console.error(err);
});

or

var db = monk(uri, opts, function(err) {
  if (err) console.error(err);
});

or

var db = monk(uri);
db.on('error', function (err) { console.error(err); })
db.on('open', function () { console.log('open'); });

Mongoskin will return an error if the connection fails:

[ERROR] [Error: failed to connect to [127.0.0.1:27017]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants