From fb44c9868c28b44e4b7cd92e142f717f027aad77 Mon Sep 17 00:00:00 2001 From: Paul Grenier Date: Tue, 22 Sep 2015 20:05:12 -0400 Subject: [PATCH] adding `try... catch...` back to handle wrapped non-async cases --- index.js | 10 +++++++--- test/test.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4f4e1bf..0c5669b 100644 --- a/index.js +++ b/index.js @@ -33,9 +33,13 @@ function compose(middleware){ function dispatch(i) { const fn = middleware[i] || next if (!fn) return Promise.resolve() - return Promise.resolve(fn(context, function next() { - return dispatch(i + 1) - })) + try { + return Promise.resolve(fn(context, function next() { + return dispatch(i + 1) + })) + } catch(err) { + return Promise.reject(err); + } } } } diff --git a/test/test.js b/test/test.js index c34f394..814bcb1 100644 --- a/test/test.js +++ b/test/test.js @@ -163,4 +163,20 @@ describe('Koa Compose', function(){ assert(called) }) }) + + it('should handle errors in wrapped non-async functions', function () { + var arr = []; + var stack = []; + + stack.push(function () { + throw new Error(); + }) + + return compose(stack.map(co.wrap))({}).then(function () { + throw 'promise was not rejected' + }) + .catch(function (e) { + e.should.be.instanceof(Error) + }) + }) })