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

Exception handling for id casting #9

Merged
merged 1 commit into from
Feb 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions lib/base-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,16 @@ BaseModel.findById = function () {
}

var collection = BaseModel.db.collection(this._collection);
var query = { _id: this._idClass(args.shift()) };
var id = args.shift();
var callback = this.resultFactory.bind(this, args.pop());
var query;

try {
query = { _id: this._idClass(id) };
}
catch (exception) {
return callback(exception);
}

args.unshift(query);
args.push(callback);
Expand All @@ -287,10 +295,18 @@ BaseModel.findByIdAndUpdate = function () {
}

var collection = BaseModel.db.collection(this._collection);
var query = { _id: this._idClass(args.shift()) };
var id = args.shift();
var update = args.shift();
var callback = this.resultFactory.bind(this, args.pop());
var options = Hoek.applyToDefaults({ new: true }, args.pop() || {});
var query;

try {
query = { _id: this._idClass(id) };
}
catch (exception) {
return callback(exception);
}

collection.findAndModify(query, undefined, update, options, callback);
};
Expand All @@ -299,7 +315,14 @@ BaseModel.findByIdAndUpdate = function () {
BaseModel.findByIdAndRemove = function (id, callback) {

var collection = BaseModel.db.collection(this._collection);
var query = { _id: this._idClass(id) };
var query;

try {
query = { _id: this._idClass(id) };
}
catch (exception) {
return callback(exception);
}

collection.remove(query, callback);
};
Expand Down
30 changes: 30 additions & 0 deletions test/lib/base-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,16 @@ lab.experiment('BaseModel Proxied Methods', function () {
});


lab.test('it catches the exception when id casting fails during findById', function (done) {

SubModel.findById('NOTVALIDOBJECTID', function (err, result) {

Code.expect(err).to.exist();
done();
});
});


lab.test('it updates a single document via id', function (done) {

var document = { name: 'New Name' };
Expand All @@ -573,6 +583,16 @@ lab.experiment('BaseModel Proxied Methods', function () {
});


lab.test('it catches the exception when id casting fails during findByIdAndUpdate', function (done) {

SubModel.findByIdAndUpdate('NOTVALIDOBJECTID', {}, function (err, result) {

Code.expect(err).to.exist();
done();
});
});


lab.test('it updates a single document via id (with options)', function (done) {

var document = { name: 'New Name' };
Expand Down Expand Up @@ -601,6 +621,16 @@ lab.experiment('BaseModel Proxied Methods', function () {
});


lab.test('it catches the exception when id casting fails during findByIdAndRemove', function (done) {

SubModel.findByIdAndRemove('NOTVALIDOBJECTID', function (err, result) {

Code.expect(err).to.exist();
done();
});
});


lab.test('it removes documents via query', function (done) {

SubModel.remove({}, function (err, result) {
Expand Down