Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
emit instead of throw async error
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Rinehart committed Jul 23, 2018
1 parent cdd3d7e commit a0f02f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ export class SubscriptionClient {
try {
JSON.parse(serializedMessage);
} catch (e) {
throw new Error(`Message must be JSON-serializable. Got: ${message}`);
this.eventEmitter.emit('error', new Error(`Message must be JSON-serializable. Got: ${message}`));
}

this.client.send(serializedMessage);
Expand All @@ -473,8 +473,8 @@ export class SubscriptionClient {
break;
default:
if (!this.reconnecting) {
throw new Error('A message was not sent because socket is not connected, is closing or ' +
'is already closed. Message was: ' + JSON.stringify(message));
this.eventEmitter.emit('error', new Error('A message was not sent because socket is not connected, is closing or ' +
'is already closed. Message was: ' + JSON.stringify(message)));
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,21 +1078,26 @@ describe('Client', function () {
wsServer.on('connection', (connection: WebSocket) => {
connection.close();
});

let errorCount = 0;
const subscriptionsClient = new SubscriptionClient(`ws://localhost:${RAW_TEST_PORT}/`, {
timeout: 500,
reconnect: true,
reconnectionAttempts: 2,
});
subscriptionsClient.onError((error) => {
expect(error.message).to.contain('A message was not sent');
errorCount += 1;
});
const connectSpy = sinon.spy(subscriptionsClient as any, 'connect');

setTimeout(() => {
expect(connectSpy.callCount).to.be.equal(2);
expect(errorCount).to.be.equal(1);
done();
}, 1500);
});

it('should stop trying to reconnect to the server if it not receives the ack', function (done) {
it('should stop trying to reconnect to the server if it does not receives the ack', function (done) {
const subscriptionsClient = new SubscriptionClient(`ws://localhost:${RAW_TEST_PORT}/`, {
timeout: 500,
reconnect: true,
Expand Down

0 comments on commit a0f02f5

Please sign in to comment.