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

pass queue errors to next middleware from view #1257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var utils = require('keystone-utils');
* @api public
*/

function View (req, res) {
function View (req, res, next) {

if (!req || req.constructor.name !== 'IncomingMessage') {
throw new Error('Keystone.View Error: Express request object is required.');
Expand All @@ -28,6 +28,7 @@ function View (req, res) {

this.req = req;
this.res = res;
this.next = next;

this.initQueue = []; // executed first in series
this.actionQueue = []; // executed second in parallel, if optional conditions are met
Expand Down Expand Up @@ -367,7 +368,11 @@ View.prototype.render = function (renderFn, locals, callback) {
throw new Error('Keystone.View.render() events must be functions.');
}
}, function (err) {
renderFn(err, req, res);
});
if (err && this.next) {
this.next(err);
} else {
renderFn(err, req, res);
}
}.bind(this));

};
26 changes: 26 additions & 0 deletions test/unit/lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ describe('Keystone.View', function () {
.expect('OK', done);
});

it('must return control to the next middleware on method errors', function(done){
var app = getApp();
app.get('/', function(req, res, next) {
var view = new keystone.View(req, res, next);
view.on('init', function(next){
var err = new Error('Not Found');
err.status = 404;
next(err);
});
view.render(function() {
var err = new Error('must not call render');
done(err);
});
});
app.use(function(err,req,res,next){
if(err && err.status === 404){
res.status(404).send('Not Found');
} else {
done('error should be handled');
}
});
request(app)
.get('/')
.expect(404, done)
});

function getApp_getAndPost() {
var app = getApp();
app.all('/', function (req, res) {
Expand Down