From 2dc97b67def9398602f1344ab578da3fe744a4a5 Mon Sep 17 00:00:00 2001 From: Jake Orr Date: Fri, 10 Dec 2021 13:36:22 -0800 Subject: [PATCH 1/3] Complete handler chain if res is sent This change allows `handler(res, req)` to resolve for middlewares that don't call next, but end the response. --- src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index dd0a2c3..7387fc3 100644 --- a/src/index.js +++ b/src/index.js @@ -67,7 +67,9 @@ export default function factory({ if (attachParams) req.params = params; let i = 0; const len = handlers.length; - const loop = async (next) => handlers[i++](req, res, next); + const loop = async (next) => Promise.resolve(handlers[i++](req, res, next)) + .then(() => isResSent(res) && done()) + .catch(next); const next = (err) => { i < len ? err From c837eee11775555f09ead193f324350813b567a5 Mon Sep 17 00:00:00 2001 From: Jake Orr Date: Mon, 13 Dec 2021 09:52:37 -0800 Subject: [PATCH 2/3] use try/catch + await to generalize error handling This change causes errors for handlers (either synchronous or asynchronous) to be handled in the same way. --- src/index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 7387fc3..2edea6f 100644 --- a/src/index.js +++ b/src/index.js @@ -67,9 +67,14 @@ export default function factory({ if (attachParams) req.params = params; let i = 0; const len = handlers.length; - const loop = async (next) => Promise.resolve(handlers[i++](req, res, next)) - .then(() => isResSent(res) && done()) - .catch(next); + const loop = async (next) => { + try { + await Promise.resolve(handlers[i++](req, res, next)); + isResSent(res) && done(); + } catch (err) { + next(err); + } + } const next = (err) => { i < len ? err From 0358b74facfa5d7d4f7cfcfeb0ea07690bd1372b Mon Sep 17 00:00:00 2001 From: jakeorr Date: Thu, 3 Feb 2022 11:30:10 -0800 Subject: [PATCH 3/3] remove unecessary Promise.resolve() --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 2edea6f..d9170ac 100644 --- a/src/index.js +++ b/src/index.js @@ -69,7 +69,7 @@ export default function factory({ const len = handlers.length; const loop = async (next) => { try { - await Promise.resolve(handlers[i++](req, res, next)); + await handlers[i++](req, res, next); isResSent(res) && done(); } catch (err) { next(err);