diff --git a/lib/hooks.js b/lib/hooks.js index 9596360dfb..8d95958d69 100644 --- a/lib/hooks.js +++ b/lib/hooks.js @@ -105,8 +105,12 @@ const hookMixin = exports.hookMixin = function hookMixin (service) { error }); - return processHooks - .call(service, hookChain, errorHookObject) + return processHooks.call(service, hookChain, errorHookObject) + .catch(error => { + errorHookObject.error = error; + + return errorHookObject; + }) .then(hook => { if (returnHook) { // Either resolve or reject with the hook object diff --git a/test/hooks/hooks.test.js b/test/hooks/hooks.test.js index d361d6ac32..afeb1b3224 100644 --- a/test/hooks/hooks.test.js +++ b/test/hooks/hooks.test.js @@ -182,7 +182,7 @@ describe('hooks basics', () => { }); }); - it('on argument validation error', () => { + it('on argument validation error (https://github.com/feathersjs/express/issues/19)', () => { const app = feathers().use('/dummy', { get (id) { return Promise.resolve({ id }); @@ -197,6 +197,29 @@ describe('hooks basics', () => { }); }); + it('on error in error hook (https://github.com/feathersjs/express/issues/21)', () => { + const app = feathers().use('/dummy', { + get (id) { + return Promise.reject(new Error('Nope')); + } + }); + + app.service('dummy').hooks({ + error: { + get (context) { + throw new Error('Error in error hook'); + } + } + }); + + return app.service('dummy').get(10, {}, true).catch(context => { + assert.equal(context.service, app.service('dummy')); + assert.equal(context.type, 'error'); + assert.equal(context.path, 'dummy'); + assert.equal(context.error.message, 'Error in error hook'); + }); + }); + it('still swallows error if context.result is set', () => { const result = { message: 'this is a test' }; const app = feathers().use('/dummy', {