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

Allow errors to be swallowed in error hooks #170

Merged
merged 1 commit into from
Sep 27, 2017
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
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