Skip to content

Commit

Permalink
fix(Error handling): Escalate unhandledpromiserejectionwarning in non…
Browse files Browse the repository at this point in the history
… promise methods (#3)
  • Loading branch information
rveitch authored and sebelga committed Sep 11, 2019
1 parent 6eeebe0 commit 38842c7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ class HooksPromise {
/**
* Get the target method response
*/
let responsePromised = originalMethod.apply(self, passedArgs);
let responsePromised;
try {
responsePromised = originalMethod.apply(self, passedArgs);
} catch (e) {
responsePromised = Promise.reject(e);
}

/**
* Convert it to a Promise if it is not to be able to chain it
Expand Down
3 changes: 3 additions & 0 deletions mocks/model.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module.exports = {
resolve('1234');
}),
saveReturnObject: () => Promise.resolve({ a: 123 }),
saveThrowError: () => {
throw new Error('Thrown Error');
},
};
},
};
8 changes: 8 additions & 0 deletions test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ describe('hooks-promise', () => {
expect(check2).equal('myPreHookMethod2');
});
});

it('should catch and reject thrown errors when the original method is not a promise', () => {
model.pre('saveThrowError', spies.preHook2);
return model.saveThrowError()
.catch((err) => {
expect(err.message).equal('Thrown Error');
});
});
});

describe('post()', () => {
Expand Down

0 comments on commit 38842c7

Please sign in to comment.