From af38033f83081b1fde48ecb6b2179b4cb3bb302b Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 3 Feb 2017 11:24:20 -0700 Subject: [PATCH] fix: make error handlers handle errors in pre hooks Re: Automattic/mongoose#4927 --- index.js | 9 +++++++-- test/wrap.test.js | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 4dfed03..abe6444 100644 --- a/index.js +++ b/index.js @@ -239,8 +239,13 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) { this.execPre(name, context, function(error) { if (error) { - return _handleWrapError(_this, error, name, context, argsWithoutCb, - options, lastArg) + var numCallbackParams = options.numCallbackParams || 0; + var nulls = []; + for (var i = 0; i < numCallbackParams; ++i) { + nulls.push(null); + } + return _handleWrapError(_this, error, name, context, nulls, + options, lastArg); } var end = (typeof lastArg === 'function' ? args.length - 1 : args.length); diff --git a/test/wrap.test.js b/test/wrap.test.js index b3fe7d0..f1f336e 100644 --- a/test/wrap.test.js +++ b/test/wrap.test.js @@ -130,7 +130,7 @@ describe('wrap()', function() { done(new Error('fail')); }); - hooks.post('cook', function(error, callback) { + hooks.post('cook', function(error, res, callback) { callback(new Error('another error occurred')); }); @@ -148,7 +148,7 @@ describe('wrap()', function() { }, null, args, - { useErrorHandlers: true }); + { useErrorHandlers: true, numCallbackParams: 1 }); }); it('error handlers with no callback', function(done) {