From 20e8b0226ebe33d7b92b3a4df5b114f21fed4855 Mon Sep 17 00:00:00 2001 From: Guangcheng Wei Date: Wed, 8 Feb 2023 14:03:53 -0500 Subject: [PATCH 1/3] stop calling then when next is called --- lib/chain.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/chain.js b/lib/chain.js index f1fc5caf..041b6fbf 100644 --- a/lib/chain.js +++ b/lib/chain.js @@ -221,8 +221,12 @@ function call(handler, err, req, res, _next) { } else if (!hasError && arity < 4) { // request-handling middleware process.nextTick(function nextTick() { - const result = handler(req, res, next); - if (result && typeof result.then === 'function') { + let nextCalled = false; + const result = handler(req, res, (...params) => { + nextCalled = true; + next(...params); + }); + if (!nextCalled && result && typeof result.then === 'function') { result.then(resolve, reject); } }); From b96cae815bd2cee2d17869e9e0fb3366a7f77ac2 Mon Sep 17 00:00:00 2001 From: Guangcheng Wei Date: Wed, 8 Feb 2023 17:11:44 -0500 Subject: [PATCH 2/3] add test --- test/server.test.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/server.test.js b/test/server.test.js index 4b28c2c1..538852c5 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -2994,3 +2994,38 @@ test('req and res should use own logger by if set during .first', function(t) { t.end(); }); }); + +test('should not call then when next called', function(t) { + let counter = 0; + SERVER.use(function first(req, res, next) { + next(); + return Promise.resolve(); + }); + + SERVER.get('/ping', function echoId(req, res, next) { + counter++; + t.equal(counter, 1); + next(); + }); + + CLIENT.get('/ping', function() { + t.end(); + }); +}); + +test('should stop after next(false)', function(t) { + let counter = 0; + SERVER.use(function first(req, res, next) { + next(false); + return Promise.resolve(); + }); + + SERVER.get('/ping', function echoId(req, res, next) { + counter++; + next(); + }); + CLIENT.get('/ping', function() { + t.equal(counter, 0); + t.end(); + }); +}); From d19e9dcbd3ac483cd50da250e919c80cabb76a4a Mon Sep 17 00:00:00 2001 From: Guangcheng Wei Date: Fri, 21 Jul 2023 17:09:59 -0400 Subject: [PATCH 3/3] fix merge --- test/server.test.js | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/test/server.test.js b/test/server.test.js index 83d1ed42..7abffb7e 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -3030,3 +3030,65 @@ test('should stop after next(false)', function(t) { t.end(); }); }); + +test('should throw if handleUncaughtExceptions is invalid', function(t) { + t.throws(() => { + restify.createServer({ + handleUncaughtExceptions: 'this is invalid' + }); + }); + t.end(); +}); + +test('should use custom function for error handling', function(t) { + const asl = new AsyncLocalStorage(); + let callOnError; + var server = restify.createServer({ + dtrace: helper.dtrace, + strictNext: true, + handleUncaughtExceptions: (req, res, onError, next) => { + callOnError = err => { + const newErr = new Error('new error'); + newErr.orig = err; + onError(newErr); + }; + asl.run({}, next, req, res); + }, + log: helper.getLog('server') + }); + var client; + var port; + + server.listen(PORT + 1, '127.0.0.1', function() { + port = server.address().port; + client = restifyClients.createJsonClient({ + url: 'http://127.0.0.1:' + port, + dtrace: helper.dtrace, + retry: false + }); + + const expectedErr = new Error('foo'); + server.get('/throw', function(req, res, next) { + // We don't really need to throw to test, and catching the throw is + // hard because nodeunit messes with uncaughtException event + callOnError(expectedErr); + }); + + server.on('uncaughtException', function(req, res, route, err) { + t.ok(err); + t.strictEqual(err.orig, expectedErr); + t.equal(err.message, 'new error'); + res.send(err); + }); + + client.get('/throw', function(err, _, res) { + t.ok(err); + t.equal(res.statusCode, 500); + + client.close(); + server.close(function() { + t.end(); + }); + }); + }); +});