Skip to content

Commit

Permalink
fix: add method and path to AsyncError
Browse files Browse the repository at this point in the history
  • Loading branch information
ghermeto committed Jul 7, 2020
1 parent e76f888 commit 681ca57
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,14 @@ function call(handler, err, req, res, _next) {
function reject(error) {
if (!(error instanceof Error)) {
error = new customErrorTypes.AsyncError(
{ info: { cause: error, handler: handler._name } },
{
info: {
cause: error,
handler: handler._name,
method: req.method,
path: req.path ? req.path() : undefined
}
},
'Async middleware rejected without an error'
);
}
Expand Down
3 changes: 3 additions & 0 deletions test/chain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ test('abort with rejected promise without error', function(t) {
endHandlerTimer: function() {},
closed: function() {
return false;
},
path: function() {
return '/';
}
},
{},
Expand Down
25 changes: 25 additions & 0 deletions test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2903,6 +2903,31 @@ test('async prerouting chain with error', function(t) {
});
});

test('async prerouting chain with empty rejection', function(t) {
SERVER.pre(async function(req, res) {
await helper.sleep(10);
return Promise.reject();
});

SERVER.get('/hello/:name', function tester(req, res, next) {
res.send(req.params.name);
next();
});

SERVER.on('Async', function(req, res, err, callback) {
t.equal(err.jse_info.cause, undefined);
t.equal(err.jse_info.method, 'GET');
t.equal(err.jse_info.path, '/hello/mark');
callback();
});

CLIENT.get('/hello/mark', function(err, _, res) {
t.ok(err);
t.equal(res.statusCode, 500);
t.end();
});
});

test('async use chain with error', function(t) {
SERVER.use(async function(req, res) {
await helper.sleep(10);
Expand Down

0 comments on commit 681ca57

Please sign in to comment.