Skip to content

Commit 30aa55c

Browse files
authored
Merge pull request #542 from GetStream/ignore-already-exists-error
fix: ignore already exists error for message resend
2 parents 8032a1e + 19cbc5b commit 30aa55c

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

projects/stream-chat-angular/src/lib/channel.service.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,38 @@ describe('ChannelService', () => {
14361436
expect(latestMessage.status).toBe('received');
14371437
});
14381438

1439+
it('should ignore error if resend failed because message already exists', async () => {
1440+
await init();
1441+
let latestMessage!: StreamMessage;
1442+
service.activeChannelMessages$.subscribe((m) => {
1443+
latestMessage = m[m.length - 1];
1444+
});
1445+
latestMessage.status = 'failed';
1446+
let channel!: Channel<DefaultStreamChatGenerics>;
1447+
service.activeChannel$.pipe(first()).subscribe((c) => (channel = c!));
1448+
spyOn(channel, 'sendMessage').and.rejectWith({
1449+
code: 4,
1450+
status: 400,
1451+
response: {
1452+
data: {
1453+
message:
1454+
'SendMessage failed with error: "a message with ID zitaszuperagetstreamio-9005f6d8-aeb7-42a1-984c-921f94567759 already exists"',
1455+
},
1456+
},
1457+
});
1458+
spyOn(channel.state, 'addMessageSorted').and.callThrough();
1459+
await service.resendMessage(latestMessage);
1460+
1461+
expect(channel.state.addMessageSorted).toHaveBeenCalledWith(
1462+
jasmine.objectContaining({
1463+
status: 'received',
1464+
}),
1465+
true
1466+
);
1467+
1468+
expect(latestMessage.status).toBe('received');
1469+
});
1470+
14391471
it('should set message state while sending', async () => {
14401472
await init();
14411473
let channel!: Channel<DefaultStreamChatGenerics>;

projects/stream-chat-angular/src/lib/channel.service.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ export class ChannelService<
801801
},
802802
true
803803
);
804-
await this.sendMessageRequest(message);
804+
return this.sendMessageRequest(message, undefined, true);
805805
}
806806

807807
/**
@@ -1009,7 +1009,8 @@ export class ChannelService<
10091009

10101010
private async sendMessageRequest(
10111011
preview: MessageResponse<T> | StreamMessage<T>,
1012-
customData?: Partial<T['messageType']>
1012+
customData?: Partial<T['messageType']>,
1013+
isResend = false
10131014
) {
10141015
const channel = this.activeChannelSubject.getValue()!;
10151016
const isThreadReply = !!preview.parent_id;
@@ -1046,17 +1047,32 @@ export class ChannelService<
10461047
.subscribe((m) => (messages = m));
10471048
const newMessage = messages[messages.length - 1]!;
10481049
return newMessage;
1049-
} catch (error) {
1050+
} catch (error: any) {
10501051
const stringError = JSON.stringify(error);
1051-
const parsedError: { status?: number } = stringError
1052-
? (JSON.parse(stringError) as { status?: number })
1053-
: {};
1052+
const parsedError: {
1053+
status?: number;
1054+
code?: number;
1055+
response?: { data?: { message?: string } };
1056+
} = stringError ? (JSON.parse(stringError) as { status?: number }) : {};
1057+
1058+
let isAlreadyExists = false;
1059+
if (isResend) {
1060+
if (
1061+
parsedError.status === 400 &&
1062+
parsedError.code === 4 &&
1063+
parsedError?.response?.data?.message?.includes('already exists')
1064+
) {
1065+
isAlreadyExists = true;
1066+
}
1067+
}
10541068

10551069
channel.state.addMessageSorted(
10561070
{
10571071
...(preview as MessageResponse<T>),
1058-
errorStatusCode: parsedError.status || undefined,
1059-
status: 'failed',
1072+
errorStatusCode: isAlreadyExists
1073+
? undefined
1074+
: parsedError.status || undefined,
1075+
status: isAlreadyExists ? 'received' : 'failed',
10601076
},
10611077
true
10621078
);

0 commit comments

Comments
 (0)