8000 Properly resolve the promise in error hooks if returnHook is set. by daffl · Pull Request #769 · 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
31 changes: 14 additions & 17 deletions lib/hooks.js
B95B
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const {
hooks,
validateArguments,
isPromise
} = require('@feathersjs/commons');
const { hooks, validateArguments, isPromise } = require('@feathersjs/commons');

const {
createHookObject, getHooks, processHooks, enableHooks, makeArguments
createHookObject,
getHooks,
processHooks,
enableHooks,
makeArguments
} = hooks;

// A service mixin that adds `service.hooks()` method and functionality
Expand Down Expand Up @@ -48,7 +48,6 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
// Create the hook object that gets passed through
const hookObject = createHookObject(method, args, {
type: 'before', // initial hook object type
returnHook,
service,
app
});
Expand Down Expand Up @@ -89,8 +88,8 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
})
.then(hookObject =>
// Finally, return the result
// Or the hook object if the `__returnHook` flag is set
hookObject.returnHook ? hookObject : hookObject.result
// Or the hook object if the `returnHook` flag is set
returnHook ? hookObject : hookObject.result
)
// Handle errors
.catch(error => {
Expand All @@ -110,16 +109,14 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
return processHooks
.call(service, hookChain, errorHookObject)
.then(hook => {
if (errorHookObject.returnHook) {
// Return either the complete hook if the `__returnHook` flag is set
return Promise.reject(hook);
} else if (hook.result) {
// Return the result if it is set so you can swallow errors
return Promise.resolve(hook.result);
if (returnHook) {
// Either resolve or reject with the hook object
return hook.result ? hook : Promise.reject(hook);
}

// If none of the above, return the error
return Promise.reject(hook.error);
// Otherwise return either the result if set (to swallow errors)
// Or reject with the hook error
return hook.result ? hook.result : Promise.reject(hook.error);
});
});
};
Expand Down
22 changes: 22 additions & 0 deletions test/hooks/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,27 @@ describe('hooks basics', () => {
assert.equal(context.error.message, 'Something went wrong');
});
});

it('still swallows error if context.result is set', () => {
const result = { message: 'this is a test' };
const app = feathers().use('/dummy', {
get () {
return Promise.reject(new Error('Something went wrong'));
}
});

app.service('dummy').hooks({
error (context) {
context.result = result;
}
});

return app.service('dummy').get(10, {}, true).then(hook => {
assert.ok(hook.error);
assert.deepEqual(hook.result, result);
}).catch(() => {
throw new Error('Should never get here');
});
});
});
});
0