Skip to content

Commit

Permalink
Merge pull request #3358 from turtleDev/master
Browse files Browse the repository at this point in the history
fix server not propagating errors on prehandler(promise) + handler error (#3242)
  • Loading branch information
hueniverse committed Nov 29, 2016
2 parents 9676577 + b4aeaf7 commit 10bcdaa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ internals.prerequisites = function (request, callback) {
}, nextSet);
};

// save a reference to the current domain, for use in the callback below

const domain = process.domain;

Items.serial(request._route._prerequisites, each, (err) => {

if (err) {
return callback(err);
}

return internals.handler(request, callback);
const wrapped = domain.bind(internals.handler);
return wrapped(request, callback);
});
};

Expand Down
4 changes: 2 additions & 2 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ internals.Response.prototype._prepare = function (next) {
return this._processPrepare(next);
}

const onDone = (source) => {
const onDone = Hoek.nextTick((source) => {

if (source instanceof Error) {
return next(Boom.wrap(source));
Expand All @@ -466,7 +466,7 @@ internals.Response.prototype._prepare = function (next) {
this._setSource(source);
this._passThrough();
this._processPrepare(next);
};
});

const onError = (source) => {

Expand Down
47 changes: 47 additions & 0 deletions test/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,53 @@ describe('handler', () => {
});
});

it('returns error if prerequisite returns promise, and handler throws an error', (done) => {

const pre1 = function (request, reply) {

return reply('Hello');
};

const pre2 = function (request, reply) {

return reply(Promise.resolve('world'));
};

const handler = function (request, reply) {

throw new Error();
};


const server = new Hapi.Server();
server.connection();
server.route({
method: 'GET',
path: '/',
config: {
pre: [
[{ method: pre1, assign: 'm1' }],
{ method: pre2, assign: 'm2' }
],
handler
}
});

const orig = console.error;
console.error = function () {

console.error = orig;
expect(arguments[0]).to.equal('Debug:');
expect(arguments[1]).to.equal('internal, implementation, error');
};

server.inject('/', (res) => {

expect(res.result.statusCode).to.equal(500);
done();
});
});

it('passes wrapped object', (done) => {

const pre = function (request, reply) {
Expand Down

0 comments on commit 10bcdaa

Please sign in to comment.