Implement ModelCtor.afterRemoteError

This commit is contained in:
Miroslav Bajtoš 2015-04-03 10:31:03 +02:00
parent a71c8253e2
commit dd83be99f0
3 changed files with 27 additions and 1 deletions

View File

@ -200,6 +200,14 @@ module.exports = function(registry) {
}); });
}; };
ModelCtor.afterRemoteError = function(name, fn) {
var className = this.modelName;
this._runWhenAttachedToApp(function(app) {
var remotes = app.remotes();
remotes.afterError(className + '.' + name, fn);
});
};
ModelCtor._runWhenAttachedToApp = function(fn) { ModelCtor._runWhenAttachedToApp = function(fn) {
if (this.app) return fn(this.app); if (this.app) return fn(this.app);
var self = this; var self = this;

View File

@ -48,7 +48,7 @@
"nodemailer-stub-transport": "^0.1.5", "nodemailer-stub-transport": "^0.1.5",
"serve-favicon": "^2.2.0", "serve-favicon": "^2.2.0",
"stable": "^0.1.5", "stable": "^0.1.5",
"strong-remoting": "^2.13.2", "strong-remoting": "^2.15.0",
"uid2": "0.0.3", "uid2": "0.0.3",
"underscore.string": "^3.0.3" "underscore.string": "^3.0.3"
}, },

View File

@ -219,6 +219,24 @@ describe.onServer('Remote Methods', function() {
}); });
}); });
describe('Model.afterRemoteError(name, fn)', function() {
it('runs the function when method fails', function(done) {
var actualError = 'hook not called';
User.afterRemoteError('login', function(ctx, next) {
actualError = ctx.error;
next();
});
request(app).get('/users/sign-in?username=bob&password=123')
.end(function(err, res) {
if (err) return done(err);
expect(actualError)
.to.have.property('message', 'bad username and password!');
done();
});
});
});
describe('Remote Method invoking context', function() { describe('Remote Method invoking context', function() {
describe('ctx.req', function() { describe('ctx.req', function() {
it('The express ServerRequest object', function(done) { it('The express ServerRequest object', function(done) {