Skip to content

Commit

Permalink
Merge pull request #1272 from strongloop/feature/after-remote-error-hook
Browse files Browse the repository at this point in the history
Model.afterRemoteError hook
  • Loading branch information
bajtos committed Apr 7, 2015
2 parents 93960b8 + dd83be9 commit b61fae5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 40 deletions.
18 changes: 8 additions & 10 deletions common/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,17 +626,15 @@ module.exports = function(User) {
}
);

UserModel.on('attached', function() {
UserModel.afterRemote('confirm', function(ctx, inst, next) {
if (ctx.args.redirect !== undefined) {
if (!ctx.res) {
return next(new Error('The transport does not support HTTP redirects.'));
}
ctx.res.location(ctx.args.redirect);
ctx.res.status(302);
UserModel.afterRemote('confirm', function(ctx, inst, next) {
if (ctx.args.redirect !== undefined) {
if (!ctx.res) {
return next(new Error('The transport does not support HTTP redirects.'));
}
next();
});
ctx.res.location(ctx.args.redirect);
ctx.res.status(302);
}
next();
});

// default models
Expand Down
57 changes: 28 additions & 29 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,36 +187,40 @@ module.exports = function(registry) {

// before remote hook
ModelCtor.beforeRemote = function(name, fn) {
var self = this;
if (this.app) {
var remotes = this.app.remotes();
var className = self.modelName;
var className = this.modelName;
this._runWhenAttachedToApp(function(app) {
var remotes = app.remotes();
remotes.before(className + '.' + name, function(ctx, next) {
fn(ctx, ctx.result, next);
});
} else {
var args = arguments;
this.once('attached', function() {
self.beforeRemote.apply(self, args);
});
}
});
};

// after remote hook
ModelCtor.afterRemote = function(name, fn) {
var self = this;
if (this.app) {
var remotes = this.app.remotes();
var className = self.modelName;
var className = this.modelName;
this._runWhenAttachedToApp(function(app) {
var remotes = app.remotes();
remotes.after(className + '.' + name, function(ctx, next) {
fn(ctx, ctx.result, next);
});
} else {
var args = arguments;
this.once('attached', function() {
self.afterRemote.apply(self, args);
});
}
});
};

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) {
if (this.app) return fn(this.app);
var self = this;
self.once('attached', function() {
fn(self.app);
});
};

// resolve relation functions
Expand Down Expand Up @@ -383,15 +387,10 @@ module.exports = function(registry) {
*/

Model.getApp = function(callback) {
var Model = this;
if (this.app) {
callback(null, this.app);
} else {
Model.once('attached', function() {
assert(Model.app);
callback(null, Model.app);
});
}
this._runWhenAttachedToApp(function(app) {
assert(Model.app);
callback(null, Model.app);
});
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"nodemailer-stub-transport": "^0.1.5",
"serve-favicon": "^2.2.0",
"stable": "^0.1.5",
"strong-remoting": "^2.13.2",
"strong-remoting": "^2.15.0",
"uid2": "0.0.3",
"underscore.string": "^3.0.3"
},
Expand Down
18 changes: 18 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('ctx.req', function() {
it('The express ServerRequest object', function(done) {
Expand Down

0 comments on commit b61fae5

Please sign in to comment.