afterDestroy not called on adapter error

Similar to afterCreate and afterUpdate, we don't want to run the afterXXXX handlers if the delete failed according to the adapter.
This commit is contained in:
Scott Nonnenberg 2013-03-30 17:18:47 -07:00
parent 13ac3cf088
commit 2ba862f5af
2 changed files with 21 additions and 1 deletions

View File

@ -721,8 +721,12 @@ AbstractClass.prototype.destroy = function (cb) {
this.trigger('destroy', function (destroyed) { this.trigger('destroy', function (destroyed) {
this._adapter().destroy(this.constructor.modelName, this.id, function (err) { this._adapter().destroy(this.constructor.modelName, this.id, function (err) {
if (err) {
return cb(err);
}
destroyed(function () { destroyed(function () {
if(cb) cb(err); if(cb) cb();
}); });
}.bind(this)); }.bind(this));
}); });

View File

@ -221,6 +221,22 @@ describe('hooks', function() {
user.destroy(); user.destroy();
}); });
}); });
it('afterDestroy should not be triggered on failed destroy', function(done) {
var old = User.schema.adapter.destroy;
User.schema.adapter.destroy = function(modelName, id, cb) {
cb(new Error('error'));
}
User.afterDestroy = function() {
throw new Error('shouldn\'t be called')
};
User.create(function (err, user) {
user.destroy(function(err) {
User.schema.adapter.destroy = old;
done();
});
});
});
}); });
describe('lifecycle', function() { describe('lifecycle', function() {