Skip to content

Commit d26a6e5

Browse files
Merge pull request #335 from purecloudlabs/STREAM-949
[STREAM-949] - Catch and emit error in `handleStanzaDisconnectedEvent`
2 parents 3a89466 + e899d13 commit d26a6e5

6 files changed

Lines changed: 115 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
# [Unreleased](https://github.com/purecloudlabs/genesys-cloud-streaming-client/compare/v19.5.0...HEAD)
8+
### Added
9+
* [STREAM-949](https://inindca.atlassian.net/browse/STREAM-949) - Catch errors and emit them as a `disconnected` event and include error.
810

911
# [v19.5.0](https://github.com/purecloudlabs/genesys-cloud-streaming-client/compare/v19.4.1...v19.5.0)
1012
### Added

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// loadScript('test/scripts/happy-path.js');
3030
// loadScript('test/scripts/duplicate-channel-connection.js');
3131
// loadScript('test/scripts/send-message.js');
32+
// loadScript('test/scripts/token-becoming-invalid.js');
3233
// loadScript('test/scripts/401.js');
3334
// loadScript('test/scripts/custom-logger.js');
3435
// loadScript('test/scripts/connect-with-retry.js');
@@ -39,4 +40,4 @@
3940

4041
</body>
4142

42-
</html>
43+
</html>

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,18 @@ export class Client extends EventEmitter {
259259
this.emit('disconnected', { reconnecting: this.autoReconnect });
260260

261261
if (this.autoReconnect) {
262-
return this.connect({ keepTryingOnFailure: true });
262+
return this.connect({ keepTryingOnFailure: true })
263+
.catch(error => {
264+
this.logger.error('Failed to auto reconnect', {
265+
keepTryingOnFailure: true,
266+
stanzaInstanceId: disconnectedInstance.id,
267+
channelId: disconnectedInstance.channelId
268+
});
269+
this.emit('disconnected', {
270+
error,
271+
reconnecting: false
272+
});
273+
});
263274
}
264275
}
265276

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
let client;
2+
3+
const button = document.createElement('button');
4+
button.onclick = () => {
5+
console.log('button clicked, going to send message');
6+
const barejid = client.activeStanzaInstance?.jid?.match(/(.+\.com)/)?.[1];
7+
const message = {
8+
to: barejid,
9+
from: client.activeStanzaInstance?.jid,
10+
mediaMessage: { id: '123', method: 'headsetControlsRequest', params: { requestType: 'mediaHelper' }}
11+
}
12+
client.activeStanzaInstance?.sendMessage(message);
13+
};
14+
button.textContent = 'Send message';
15+
document.body.appendChild(button);
16+
17+
const button2 = document.createElement('button');
18+
button2.textContent = 'Invalidate token';
19+
button2.onclick = async () => {
20+
console.log('button clicked, invalidating auth token');
21+
client.setAccessToken('');
22+
await client.disconnect();
23+
client.connect({ keepTryingOnFailure: true });
24+
}
25+
document.body.appendChild(button2);
26+
27+
28+
const authButon = document.createElement('button');
29+
authButon.textContent = 'Set auth token';
30+
authButon.onclick = () => {
31+
const input = document.querySelector('input');
32+
const token = input?.value;
33+
console.log('auth token', token);
34+
window.scConfig = {
35+
authToken: token,
36+
host: 'wss://streaming.inindca.com',
37+
optOutOfWebrtcStatsTelemetry: true
38+
};
39+
client = new window.GenesysCloudStreamingClient(window.scConfig);
40+
console.log('client');
41+
window.client = client;
42+
client.connect({ keepTryingOnFailure: true })
43+
.then(() => {
44+
console.log('Client connected 1');
45+
});
46+
47+
client.on('error', (error) => {
48+
console.log('RECEIVED Streaming Client Error!!!!', error);
49+
});
50+
51+
client.on('disconnected', (e) => {
52+
console.log('Client disconnected');
53+
console.log(e);
54+
})
55+
}
56+
document.body.appendChild(authButon);
57+
58+
const authInput = document.createElement('input');
59+
authInput.type = 'text';
60+
document.body.appendChild(authInput);
61+
62+
const clearAccessTokenBtn = document.createElement('button');
63+
clearAccessTokenBtn.textContent = 'Clear access token';
64+
clearAccessTokenBtn.onclick = () => {
65+
window.client.setAccessToken('');
66+
}
67+
document.body.appendChild(clearAccessTokenBtn);
68+
69+
70+
const disconnect = document.createElement('button');
71+
disconnect.textContent = 'Disconnect';
72+
disconnect.onclick = () => {
73+
window.client.disconnect();
74+
}
75+
document.body.appendChild(disconnect);
76+
77+
78+
//
79+
80+
81+

test/unit/client.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,22 @@ describe('handleStanzaDisconnectedEvent', () => {
13611361
expect(connectSpy).toHaveBeenCalled();
13621362
});
13631363

1364+
it('should catch reconnection errors and emit them', async () => {
1365+
client['autoReconnect'] = true;
1366+
1367+
const err = {message: 'AXIOS 401 for example'}
1368+
const errorForThrowing = new StreamingClientError(StreamingClientErrorTypes.invalid_token, 'the error', err);
1369+
1370+
connectSpy = client.connect = jest.fn().mockRejectedValue(errorForThrowing);
1371+
const emitSpy = jest.spyOn(client, 'emit');
1372+
1373+
await client['handleStanzaDisconnectedEvent'](fakeStanza);
1374+
1375+
expect(client.connected).toBeFalsy();
1376+
expect(emitSpy).toHaveBeenCalledTimes(2);
1377+
expect(emitSpy).toHaveBeenNthCalledWith(2, 'disconnected', { error: errorForThrowing, reconnecting: false });
1378+
});
1379+
13641380
it('should unproxy events', async () => {
13651381
client['autoReconnect'] = false;
13661382

0 commit comments

Comments
 (0)