Skip to content

Commit 4dc5f4c

Browse files
committed
pass queue errors to next middleware from view
1 parent 89f955f commit 4dc5f4c

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

lib/view.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var utils = require('keystone-utils');
1616
* @api public
1717
*/
1818

19-
function View(req, res) {
19+
function View(req, res, next) {
2020

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

2929
this.req = req;
3030
this.res = res;
31+
this.next = next;
3132

3233
this.initQueue = []; // executed first in series
3334
this.actionQueue = []; // executed second in parallel, if optional conditions are met
@@ -369,7 +370,11 @@ View.prototype.render = function(renderFn, locals, callback) {
369370
throw new Error('Keystone.View.render() events must be functions.');
370371
}
371372
}, function(err) {
372-
renderFn(err, req, res);
373-
});
373+
if(err && this.next) {
374+
this.next(err);
375+
} else {
376+
renderFn(err, req, res);
377+
}
378+
}.bind(this));
374379

375380
};

test/lib/view_test.js

+26
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,32 @@ describe('Keystone.View', function() {
8383
.expect('OK', done);
8484
});
8585

86+
it('must return control to the next middleware on method errors', function(done){
87+
var app = getApp();
88+
app.get('/', function(req, res, next) {
89+
var view = new keystone.View(req, res, next);
90+
view.on('init', function(next){
91+
var err = new Error('Not Found');
92+
err.status = 404;
93+
next(err);
94+
});
95+
view.render(function() {
96+
var err = new Error('must not call render');
97+
done(err);
98+
});
99+
});
100+
app.use(function(err,req,res,next){
101+
if(err && err.status === 404){
102+
res.status(404).send('Not Found');
103+
} else {
104+
done('error should be handled');
105+
}
106+
});
107+
request(app)
108+
.get('/')
109+
.expect(404, done)
110+
});
111+
86112
function getApp_getAndPost() {
87113
var app = getApp();
88114
app.all('/', function(req, res) {

0 commit comments

Comments
 (0)