Skip to content

Commit

Permalink
fix: emit after event with proper error param for node versions >= 11…
Browse files Browse the repository at this point in the history
….4.0 (#1732)
  • Loading branch information
misterdjules committed Jan 3, 2019
1 parent df3b5b0 commit 7a1378b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/server.js
Expand Up @@ -1258,8 +1258,14 @@ Server.prototype._finishReqResCycle = function _finishReqResCycle(
// after event has signature of function(req, res, route, err) {...}
self.emit('after', req, res, route, err || res.err);
} else {
// preserve error for actual finish
res.err = err;
// Store error for when the response is flushed and we actually emit the
// 'after' event. The "err" object passed to this method takes
// precedence, but in case it's not set, "res.err" may have been already
// set by another code path and we want to preserve it. The caveat thus
// is that the 'after' event will be emitted with the latest error that
// was set before the response is fully flushed. While not ideal, this
// is on purpose and accepted as a reasonable trade-off for now.
res.err = err || res.err;
}
};

Expand Down

0 comments on commit 7a1378b

Please sign in to comment.