Skip to content

Commit

Permalink
Support reply(err, result). Closes #1280
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Jan 2, 2014
1 parent facda30 commit 0e1e68f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
11 changes: 6 additions & 5 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ internals.replyInterface = function (request, finalize, base) {

finalize = Utils.once(finalize);

var root = function (result) {
var root = function (err, result) {

return internals.wrap(result, request, finalize);
return internals.wrap(err || result, request, finalize);
};

var reply = base || root;
if (base) {
base.root = root;
base._root = root;
}

var viewsManager = (base && base.env && base.env.views) || request._route.env.views || request.server._views;
Expand Down Expand Up @@ -343,9 +343,10 @@ exports.invoke = function (request, event, callback) {

Ext.runProtected(request, event, next, function (enter, exit) {

var finalize = function (result) {
var finalize = function (err, result) {

return (result === undefined ? exit() : reply.root(result));
result = err || result;
return (result === undefined ? exit() : reply._root(result));
};

finalize.env = ext.env;
Expand Down
19 changes: 18 additions & 1 deletion test/integration/ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ describe('Ext', function () {
});
});

it('replies with error using reply(null, result)', function (done) {

var server = new Hapi.Server();
server.ext('onRequest', function (request, next) {

return next(null, Hapi.error.badRequest('boom'));
});

server.route({ method: 'GET', path: '/', handler: function (request, reply) { reply('ok'); } });

server.inject({ method: 'GET', url: '/' }, function (res) {

expect(res.result.message).to.equal('boom');
done();
});
});

it('replies with a view', function (done) {

var server = new Hapi.Server({
Expand Down Expand Up @@ -92,7 +109,7 @@ describe('Ext', function () {
var server = new Hapi.Server();
server.ext('onPreResponse', function (request, next) {

return next(request.response().response.statusCode);
return next(null, request.response().response.statusCode);
});

server.inject({ method: 'GET', url: '/missing' }, function (res) {
Expand Down
23 changes: 23 additions & 0 deletions test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,29 @@ describe('Response', function () {
done();
});
});

it('uses reply(null, result) for result', function (done) {

var server = new Hapi.Server();
server.route({ method: 'GET', path: '/', handler: function (request, reply) { reply(null, 'steve'); } });
server.inject('/', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.result).to.equal('steve');
done();
});
});

it('uses reply(null, err) for err', function (done) {

var server = new Hapi.Server();
server.route({ method: 'GET', path: '/', handler: function (request, reply) { reply(null, Hapi.error.badRequest()); } });
server.inject('/', function (res) {

expect(res.statusCode).to.equal(400);
done();
});
});
});

describe('Buffer', function () {
Expand Down

0 comments on commit 0e1e68f

Please sign in to comment.