Skip to content

Commit

Permalink
fix(rpc/handler): add error catch behavior with test case
Browse files Browse the repository at this point in the history
  • Loading branch information
CheerlessCloud committed Sep 28, 2018
1 parent 920ad4f commit 15e961f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/rpc/Handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export default class RpcHandler {

async handle(): ?Object {}

handleFail(err: Error) {
// reply
// reject
async handleFail(error: Error) {
await this.reply({ error });
await this._message.reject();
}

async handleSuccess(replyPayload: ?Object) {
Expand Down
32 changes: 32 additions & 0 deletions src/rpc/Handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,38 @@ test('correct reply at positive execute case', async t => {
t.false(messageStub.reject.called);
});

test('correct reply when exception throwed in handler', async t => {
const { AwesomeHandler, serviceStub, messageStub } = t.context;
const error = new Error('My awesome error');

const handler = new AwesomeHandler({
service: serviceStub,
message: messageStub,
});
stub(handler, 'handle').rejects(error);

const handleSuccessSpy = spy(handler, 'handleSuccess');
const handleFailSpy = spy(handler, 'handleFail');
const onFailSpy = spy(handler, 'onFail');
const replySpy = spy(handler, 'reply');

await t.notThrows(handler.execute());

t.false(handleSuccessSpy.called);

t.true(handleFailSpy.calledOnceWith(error));

t.true(replySpy.calledOnce);
t.deepEqual(replySpy.firstCall.args.pop(), { error });
t.true(t.context.adapterSendStub.calledOnce);

t.false(messageStub.ack.calledOnce);
t.true(messageStub.reject.calledOnce);

t.true(onFailSpy.calledOnce);
t.true(onFailSpy.calledAfter(handleFailSpy));
});

test('must override handle method', async t => {
const { serviceStub, messageStub } = t.context;
const HandlerClass = class AwesomeHandler2 extends Handler {};
Expand Down

0 comments on commit 15e961f

Please sign in to comment.