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

avoid generating uncaught exceptions in app #93

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 14 additions & 9 deletions lib/connect-mongo.js
Expand Up @@ -149,7 +149,7 @@ module.exports = function(connect) {
} else if (self.db.openCalled) {
self.db.collection(self.db_collection_name, function(err, collection) {
if (err) {
throw new Error('Error getting collection: ' + self.db_collection_name);
return callback(new Error('Error getting collection: ' + self.db_collection_name));
} else {
self.collection = collection;

Expand All @@ -158,10 +158,10 @@ module.exports = function(connect) {
// mongo to remove anything expired without any additional delay.
self.collection.ensureIndex({expires: 1}, {expireAfterSeconds: 0}, function(err, result) {
if (err) {
throw new Error('Error setting TTL index on collection : ' + self.db_collection_name);
return callback(new Error('Error setting TTL index on collection : ' + self.db_collection_name));
}

callback && callback(self.collection);
callback && callback(null, self.collection);
});
}
});
Expand All @@ -175,7 +175,7 @@ module.exports = function(connect) {
if (err) {
if (!(err instanceof Error)) { err = new Error(String(err)); }
err.message = 'Error connecting to database: ' + err.message;
throw err;
return cb(err);
}

if (options.username && options.password) {
Expand Down Expand Up @@ -210,7 +210,8 @@ module.exports = function(connect) {

MongoStore.prototype.get = function(sid, callback) {
var self = this;
this._get_collection(function(collection) {
this._get_collection(function(err, collection) {
if (err) return callback(err);
collection.findOne({_id: sid}, function(err, session) {
try {
if (err) {
Expand Down Expand Up @@ -261,7 +262,8 @@ module.exports = function(connect) {
s.expires = new Date(today.getTime() + this.defaultExpirationTime);
}

this._get_collection(function(collection) {
this._get_collection(function(err, collection) {
if (err) return callback(err);
collection.update({_id: sid}, s, {upsert: true, safe: true}, function(err, data) {
if (err) {
callback && callback(err);
Expand All @@ -284,7 +286,8 @@ module.exports = function(connect) {
*/

MongoStore.prototype.destroy = function(sid, callback) {
this._get_collection(function(collection) {
this._get_collection(function(err, collection) {
if (err) return callback(err);
collection.remove({_id: sid}, function() {
callback && callback();
});
Expand All @@ -299,7 +302,8 @@ module.exports = function(connect) {
*/

MongoStore.prototype.length = function(callback) {
this._get_collection(function(collection) {
this._get_collection(function(err, collection) {
if (err) return callback(err);
collection.count({}, function(err, count) {
if (err) {
callback && callback(err);
Expand All @@ -318,7 +322,8 @@ module.exports = function(connect) {
*/

MongoStore.prototype.clear = function(callback) {
this._get_collection(function(collection) {
this._get_collection(function(err, collection) {
if (err) return callback(err);
collection.drop(function() {
callback && callback();
});
Expand Down