Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Commit

Permalink
Allow errors to be swallowed in error hooks #151 (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
James Atherton authored and daffl committed Sep 27, 2017
1 parent 52a7a1a commit f9b41f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ function hookMixin (service) {
.catch(error => {
const errorHook = Object.assign({}, error.hook || hookObject, {
type: 'error',
result: null,
original: error.hook,
error
});

return processHooks
.call(this, hooks.error, errorHook)
.then(hook => Promise.reject(hook.error));
.then(hook => hook.result || Promise.reject(hook.error));
});
};
});
Expand Down
18 changes: 17 additions & 1 deletion test/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ describe('error hooks', () => {
assert.ok(error.third);
});
});

it('setting `hook.result` will return result', () => {
const data = {
message: 'It worked'
};

service.hooks({
error (hook) {
hook.result = data;
return Promise.resolve(hook);
}
});

return service.get(10)
.then(result => assert.deepEqual(result, data));
});
});

describe('error in hooks', () => {
Expand Down Expand Up @@ -167,7 +183,7 @@ describe('error hooks', () => {
'Original hook still set'
);
assert.equal(hook.id, 'dishes');
assert.deepEqual(hook.result, {
assert.deepEqual(hook.original.result, {
id: 'dishes',
text: 'You have to do dishes'
});
Expand Down

0 comments on commit f9b41f3

Please sign in to comment.