Skip to content

Commit

Permalink
Chat adapter error update (#2471)
Browse files Browse the repository at this point in the history
* add error emitter to typing indicator

* add proxy in stateful client for typingIndicator

* add test

* Change files

* fix name

* Update @internal-react-composites-7cc319fa-75a7-4732-b1fb-a9425738de8d.json

* update composite test script to build first

* add test to stateful suite

* fix spelling...

* remove auto build
  • Loading branch information
dmceachernmsft authored Oct 31, 2022
1 parent fbfe38e commit 4a67078
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Fix issue where typingIndicator errors were not being caught by the ChatAdapter.",
"packageName": "@internal/chat-stateful-client",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Fix issue where typingIndicator errors were not being caught by the ChatAdapter",
"packageName": "@internal/react-composites",
"email": "[email protected]",
"dependentChangeType": "patch"
}
11 changes: 11 additions & 0 deletions packages/chat-stateful-client/src/StatefulChatThreadClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@ describe('stateful chatThreadClient correctly wraps errors', () => {
);
});

test('when sendTypingNotification fails', async () => {
const chatThreadClient = createMockChatThreadClient('threadId');
chatThreadClient.sendTypingNotification = async (): Promise<boolean> => {
throw new Error('injected error');
};
const client = createMockChatClientWithChatThreadClient(chatThreadClient);
await expect(client.getChatThreadClient('threadId').sendTypingNotification()).rejects.toThrow(
new ChatError('ChatThreadClient.sendTypingNotification', new Error('injected error'))
);
});

test('when sendMessage fails', async () => {
const chatThreadClient = createMockChatThreadClient('threadId');
chatThreadClient.sendMessage = async (): Promise<any> => {
Expand Down
8 changes: 8 additions & 0 deletions packages/chat-stateful-client/src/StatefulChatThreadClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ class ProxyChatThreadClient implements ProxyHandler<ChatThreadClient> {
case 'listReadReceipts': {
return createDecoratedListReadReceipts(chatThreadClient, this._context);
}
case 'sendTypingNotification': {
return this._context.withAsyncErrorTeedToState(
async (...args: Parameters<ChatThreadClient['sendTypingNotification']>) => {
return await chatThreadClient.sendTypingNotification(...args);
},
'ChatThreadClient.sendTypingNotification'
);
}
case 'removeParticipant': {
return this._context.withAsyncErrorTeedToState(
async (...args: Parameters<ChatThreadClient['removeParticipant']>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ describe('Error is reflected in state and events', () => {
const allLoaded = await adapter.loadPreviousChatMessages(1);
expect(allLoaded).toBe(true);
});

it('when sendTypingNotification fails', async () => {
const threadClient = new StubChatThreadClient();
threadClient.sendTypingNotification = (): Promise<boolean> => {
throw new Error('injected error');
};
const adapter = await createChatAdapterWithStubs(new StubChatClient(threadClient));
const stateListener = new StateChangeListener(adapter);
const errorListener = new ErrorListener(adapter);

await expect(adapter.sendTypingIndicator()).rejects.toThrow();

expect(stateListener.onChangeCalledCount).toBe(1);
const latestError = stateListener.state.latestErrors['ChatThreadClient.sendTypingNotification'];
expect(latestError).toBeDefined();
expect(errorListener.errors.length).toBe(1);
expect(errorListener.errors[0].target).toBe('ChatThreadClient.sendTypingNotification');
});
});

const createChatAdapterWithStubs = async (chatClient: StubChatClient): Promise<ChatAdapter> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ export class AzureCommunicationChatAdapter implements ChatAdapter {
}

async sendTypingIndicator(): Promise<void> {
await this.handlers.onTyping();
await this.asyncTeeErrorToEventEmitter(async () => {
await this.handlers.onTyping();
});
}

async removeParticipant(userId: string): Promise<void> {
Expand Down

0 comments on commit 4a67078

Please sign in to comment.