E5ED Handle errors in error hooks properly by daffl · Pull Request #819 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion test/hooks/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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', {
Expand Down
0