8000 Make sure error hooks always have the original context information by daffl · Pull Request #842 · 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
2 changes: 1 addition & 1 deletion lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
const hookChain = errorHooks.concat(finallyHooks);

// A shallow copy of the hook object
const errorHookObject = Object.assign({}, error.hook || hookObject, {
const errorHookObject = Object.assign({}, error.hook, hookObject, {
type: 'error',
result: null,
original: error.hook,
Expand Down
44 changes: 44 additions & 0 deletions test/hooks/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,48 @@ describe('`error` hooks', () => {
.catch(error => assert.equal(error.message, errorMessage));
});
});

it('Error in before hook causes inter-service calls to have wrong hook context (https://github.com/feathersjs/feathers/issues/841)', () => {
const app = feathers();

let service1Params, service2Params;

app.use('/service1', {
find () {
return Promise.resolve({ message: 'service1 success' });
}
});

app.service('service1').hooks({
before (context) {
service1Params = context.params;
return Promise.reject(new Error('Error in service1 before hook'));
}
});

app.use('/service2', {
find (params) {
return app.service('/service1').find({}).then(() => {
return { message: 'service2 success' };
});
}
});

app.service('service2').hooks({
before (context) {
service2Params = context.params;
context.params.foo = 'bar';
},
error (context) {
assert.ok(service1Params !== context.params);
assert.ok(service2Params === context.params);
assert.equal(context.path, 'service2');
assert.equal(context.params.foo, 'bar');
}
});

return app.service('/service2').find().catch(error => {
assert.equal(error.message, 'Error in service1 before hook');
});
});
});
0