Skip to content

Commit

Permalink
fix(channel): emit incoming message handler error instead of printing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Sep 5, 2022
1 parent 1197ec4 commit d71efad
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/guides/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ BaseError
│ │ ChannelConnectionError
│ │ ChannelPingTimedOutError
│ │ UnexpectedChannelMessageError
│ │ ChannelIncomingMessageError
│ │ UnknownChannelStateError
└───CompilerError
Expand Down
5 changes: 2 additions & 3 deletions src/channel/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ChannelPingTimedOutError,
UnexpectedTsError,
UnknownChannelStateError,
ChannelIncomingMessageError,
} from '../utils/errors';

interface ChannelAction {
Expand Down Expand Up @@ -233,9 +234,7 @@ async function dequeueMessage(channel: Channel): Promise<void> {
try {
await handleMessage(channel, message);
} catch (error) {
console.error('Error handling incoming message:');
console.error(message);
console.error(error);
emit(channel, 'error', new ChannelIncomingMessageError(error, message));
}
}
messageQueueLocked.set(channel, false);
Expand Down
16 changes: 16 additions & 0 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,22 @@ export class UnexpectedChannelMessageError extends ChannelError {
}
}

/**
* @category exception
*/
export class ChannelIncomingMessageError extends ChannelError {
handlerError: BaseError;

incomingMessage: { [key: string]: any };

constructor(handlerError: BaseError, incomingMessage: { [key: string]: any }) {
super(handlerError.message);
this.handlerError = handlerError;
this.incomingMessage = incomingMessage;
this.name = 'ChannelIncomingMessageError';
}
}

/**
* @category exception
*/
Expand Down
36 changes: 23 additions & 13 deletions test/integration/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ import * as sinon from 'sinon';
import BigNumber from 'bignumber.js';
import { getSdk } from '.';
import {
unpackTx, buildTx, buildTxHash, encode, decode, Tag,
IllegalArgumentError, InsufficientBalanceError, ChannelConnectionError, encodeContractAddress,
unpackTx,
buildTx,
buildTxHash,
encode,
decode,
Tag,
IllegalArgumentError,
InsufficientBalanceError,
ChannelConnectionError,
encodeContractAddress,
ChannelIncomingMessageError,
UnknownChannelStateError,
} from '../../src';
import { pause } from '../../src/utils/other';
import Channel from '../../src/channel';
Expand Down Expand Up @@ -147,23 +157,23 @@ describe('Channel', () => {
responderTx.should.eql({ ...responderTx, ...expectedTxParams });
});

it('prints error on handling incoming messages', async () => {
const stub = sinon.stub(console, 'error');
const received = new Promise((resolve) => {
stub.callsFake(resolve);
it('emits error on handling incoming messages', async () => {
const getError = new Promise<ChannelIncomingMessageError>((resolve) => {
function handler(error: ChannelIncomingMessageError): void {
resolve(error);
initiatorCh.off('error', handler);
}
initiatorCh.on('error', handler);
});
send(initiatorCh, {
jsonrpc: '2.0',
method: 'not-existing-method',
params: {},
});
await received;
expect(stub.callCount).to.be.equal(3);
expect(stub.getCall(0).firstArg).to.be.equal('Error handling incoming message:');
expect(stub.getCall(1).firstArg.error.message).to.be.equal('Method not found');
expect(stub.getCall(2).firstArg.toString())
.to.be.equal('UnknownChannelStateError: State Channels FSM entered unknown state');
stub.restore();
const error = await getError;
expect(error.incomingMessage.error.message).to.be.equal('Method not found');
expect(() => { throw error.handlerError; })
.to.throw(UnknownChannelStateError, 'State Channels FSM entered unknown state');
});

it('can post update and accept', async () => {
Expand Down

0 comments on commit d71efad

Please sign in to comment.