Skip to content

Commit 5df59ce

Browse files
committed
fix: added try/catch to unhandled errors
1 parent 542dbca commit 5df59ce

File tree

17 files changed

+153
-89
lines changed

17 files changed

+153
-89
lines changed

packages/uikit-chat-hooks/src/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,14 @@ export const useGroupChannelMessagesWithCollection: UseGroupChannelMessages = (s
7171
updateRefreshing,
7272
} = useChannelMessagesReducer(options?.sortComparator);
7373

74-
const channelMarkAsRead = async (source?: CollectionEventSource) => {
75-
try {
76-
switch (source) {
77-
case CollectionEventSource.EVENT_MESSAGE_RECEIVED:
78-
case CollectionEventSource.EVENT_MESSAGE_SENT_SUCCESS:
79-
case CollectionEventSource.SYNC_MESSAGE_FILL:
80-
case undefined:
81-
await confirmAndMarkAsRead([channel]);
82-
break;
83-
}
84-
} catch (e) {
85-
Logger.warn('[useGroupChannelMessagesWithCollection/channelMarkAsRead]', e);
74+
const channelMarkAsRead = (source?: CollectionEventSource) => {
75+
switch (source) {
76+
case CollectionEventSource.EVENT_MESSAGE_RECEIVED:
77+
case CollectionEventSource.EVENT_MESSAGE_SENT_SUCCESS:
78+
case CollectionEventSource.SYNC_MESSAGE_FILL:
79+
case undefined:
80+
confirmAndMarkAsRead([channel]);
81+
break;
8682
}
8783
};
8884
const updateNewMessagesReceived = (source: CollectionEventSource, messages: SendbirdBaseMessage[]) => {
@@ -105,7 +101,7 @@ export const useGroupChannelMessagesWithCollection: UseGroupChannelMessages = (s
105101
if (isNotEmpty(failedMessages)) updateMessages(failedMessages, false, sdk.currentUser?.userId);
106102
};
107103

108-
const init = useFreshCallback(async (startingPoint: number, limit: number, callback?: () => void) => {
104+
const init = useFreshCallback((startingPoint: number, limit: number, callback?: () => void) => {
109105
if (collectionRef.current) collectionRef.current?.dispose();
110106

111107
channelMarkAsRead();

packages/uikit-chat-hooks/src/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,9 @@ export const useGroupChannelMessagesWithQuery: UseGroupChannelMessages = (sdk, c
5353
updateRefreshing,
5454
} = useChannelMessagesReducer(options?.sortComparator);
5555

56-
const channelMarkAsRead = async () => {
57-
try {
58-
await confirmAndMarkAsRead([channel]);
59-
} catch (e) {
60-
Logger.warn('[useGroupChannelMessagesWithQuery/channelMarkAsRead]', e);
61-
}
62-
};
63-
6456
const init = useFreshCallback(async (uid?: string) => {
6557
if (uid) {
66-
channelMarkAsRead();
58+
confirmAndMarkAsRead([channel]);
6759
updateNewMessages([], true, sdk.currentUser?.userId);
6860

6961
queryRef.current = createMessageQuery(channel, options);
@@ -86,7 +78,7 @@ export const useGroupChannelMessagesWithQuery: UseGroupChannelMessages = (sdk, c
8678
if (isDifferentChannel(channel, eventChannel)) return;
8779
if (isMyMessage(message, sdk.currentUser?.userId)) return;
8880

89-
channelMarkAsRead();
81+
confirmAndMarkAsRead([channel]);
9082

9183
updateMessages([message], false, sdk.currentUser?.userId);
9284
if (options?.shouldCountNewMessages?.()) {

packages/uikit-react-native/src/components/ChannelInput/VoiceMessageInput.tsx

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
createStyleSheet,
1111
useUIKitTheme,
1212
} from '@sendbird/uikit-react-native-foundation';
13-
import { conditionChaining, millsToMMSS } from '@sendbird/uikit-utils';
13+
import { Logger, conditionChaining, millsToMMSS } from '@sendbird/uikit-utils';
1414

1515
import { useLocalization } from '../../hooks/useContext';
1616
import useVoiceMessageInput from '../../hooks/useVoiceMessageInput';
@@ -32,34 +32,50 @@ const VoiceMessageInput = ({ onClose, onSend }: VoiceMessageInputProps) => {
3232
const uiColors = colors.ui.voiceMessageInput.default[state.status !== 'idle' ? 'active' : 'inactive'];
3333

3434
const onPressCancel = async () => {
35-
actions.cancel();
36-
onClose();
35+
await actions
36+
.cancel()
37+
.catch((error) => {
38+
Logger.warn('Failed to cancel voice message input', error);
39+
})
40+
.finally(() => {
41+
onClose();
42+
});
3743
};
3844

3945
const onPressSend = async () => {
40-
actions.send();
41-
onClose();
46+
await actions
47+
.send()
48+
.catch((error) => {
49+
Logger.warn('Failed to send voice message', error);
50+
})
51+
.finally(() => {
52+
onClose();
53+
});
4254
};
4355

44-
const onPressVoiceMessageAction = () => {
45-
switch (state.status) {
46-
case 'idle':
47-
actions.startRecording();
48-
break;
49-
case 'recording':
50-
if (lessThanMinimumDuration) {
51-
actions.cancel();
52-
} else {
53-
actions.stopRecording();
54-
}
55-
break;
56-
case 'recording_completed':
57-
case 'playing_paused':
58-
actions.playPlayer();
59-
break;
60-
case 'playing':
61-
actions.pausePlayer();
62-
break;
56+
const onPressVoiceMessageAction = async () => {
57+
try {
58+
switch (state.status) {
59+
case 'idle':
60+
await actions.startRecording();
61+
break;
62+
case 'recording':
63+
if (lessThanMinimumDuration) {
64+
await actions.cancel();
65+
} else {
66+
await actions.stopRecording();
67+
}
68+
break;
69+
case 'recording_completed':
70+
case 'playing_paused':
71+
await actions.playPlayer();
72+
break;
73+
case 'playing':
74+
await actions.pausePlayer();
75+
break;
76+
}
77+
} catch (error) {
78+
Logger.warn('Failed to run voice message action.', state);
6379
}
6480
};
6581
const renderActionIcon = () => {

packages/uikit-react-native/src/components/ChannelInput/index.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context';
44

55
import { createStyleSheet, useUIKitTheme } from '@sendbird/uikit-react-native-foundation';
66
import {
7+
Logger,
78
SendbirdBaseChannel,
89
SendbirdBaseMessage,
910
SendbirdFileMessage,
@@ -187,12 +188,21 @@ const ChannelInput = (props: ChannelInputProps) => {
187188
};
188189

189190
const useTypingTrigger = (text: string, channel: SendbirdBaseChannel) => {
190-
if (channel.isGroupChannel()) {
191-
useEffect(() => {
192-
if (text.length === 0) channel.endTyping();
193-
else channel.startTyping();
194-
}, [text]);
195-
}
191+
useEffect(
192+
() => {
193+
function triggerTyping() {
194+
if (channel.isGroupChannel()) {
195+
const action = text.length === 0 ? channel.endTyping : channel.startTyping;
196+
action().catch((error) => {
197+
Logger.debug('ChannelInput: Failed to trigger typing', error);
198+
});
199+
}
200+
}
201+
202+
triggerTyping();
203+
},
204+
channel.isGroupChannel() ? [text] : [],
205+
);
196206
};
197207

198208
const useTextClearOnDisabled = (setText: (val: string) => void, chatDisabled: boolean) => {

packages/uikit-react-native/src/components/GroupChannelMessageRenderer/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ const GroupChannelMessageRenderer: GroupChannelProps['Fragment']['renderMessage'
279279
onUnsubscribeStatus={voiceMessageStatusManager.unsubscribe}
280280
onUnmount={() => {
281281
if (isVoiceMessage(message) && playerService.uri === message.url) {
282-
resetPlayer();
282+
resetPlayer().catch((_) => {});
283283
}
284284
}}
285285
{...messageProps}

packages/uikit-react-native/src/components/ReactionAddons/BottomSheetReactionAddon.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context';
55
import type { BaseMessage } from '@sendbird/chat/message';
66
import { useChannelHandler } from '@sendbird/uikit-chat-hooks';
77
import { Icon, Image, createStyleSheet, useUIKitTheme } from '@sendbird/uikit-react-native-foundation';
8-
import { SendbirdBaseChannel, SendbirdBaseMessage, useUniqHandlerId } from '@sendbird/uikit-utils';
8+
import { Logger, SendbirdBaseChannel, SendbirdBaseMessage, useUniqHandlerId } from '@sendbird/uikit-utils';
99

1010
import { UNKNOWN_USER_ID } from '../../constants';
1111
import { useReaction, useSendbirdChat } from '../../hooks/useContext';
@@ -46,10 +46,15 @@ const BottomSheetReactionAddon = ({ onClose, message, channel }: Props) => {
4646
const currentUserIdx = reactionUserIds.indexOf(currentUser?.userId ?? UNKNOWN_USER_ID);
4747
const reacted = currentUserIdx > -1;
4848

49-
const onPress = () => {
50-
if (reacted) channel.deleteReaction(message, key);
51-
else channel.addReaction(message, key);
52-
onClose();
49+
const onPress = async () => {
50+
const action = reacted ? channel.deleteReaction : channel.addReaction;
51+
await action(message, key)
52+
.catch((error) => {
53+
Logger.warn('Failed to reaction', error);
54+
})
55+
.finally(() => {
56+
onClose();
57+
});
5358
};
5459

5560
return (

packages/uikit-react-native/src/components/ReactionBottomSheets/ReactionListBottomSheet.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FlatList, Pressable, View, useWindowDimensions } from 'react-native';
33
import { useSafeAreaInsets } from 'react-native-safe-area-context';
44

55
import { Image, Modal, createStyleSheet, useUIKitTheme } from '@sendbird/uikit-react-native-foundation';
6+
import { Logger } from '@sendbird/uikit-utils';
67

78
import { UNKNOWN_USER_ID } from '../../constants';
89
import type { ReactionBottomSheetProps } from './index';
@@ -53,12 +54,14 @@ const ReactionListBottomSheet = ({ visible, onClose, onDismiss, reactionCtx, cha
5354
<View style={styles.emojiItem}>
5455
<Pressable
5556
key={key}
56-
onPress={() => {
57+
onPress={async () => {
5758
if (message && channel) {
58-
if (reacted) channel.deleteReaction(message, key);
59-
else channel.addReaction(message, key);
59+
const action = reacted ? channel.deleteReaction : channel.addReaction;
60+
action(message, key).catch((error) => {
61+
Logger.warn('Failed to reaction', error);
62+
});
6063
}
61-
onClose();
64+
await onClose();
6265
}}
6366
style={({ pressed }) => [
6467
styles.button,

packages/uikit-react-native/src/components/ThreadParentMessageRenderer/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const ThreadParentMessageRenderer = (props: ThreadParentMessageRendererProps) =>
178178
durationMetaArrayKey={VOICE_MESSAGE_META_ARRAY_DURATION_KEY}
179179
onUnmount={() => {
180180
if (isVoiceMessage(parentMessage) && playerService.uri === parentMessage.url) {
181-
resetPlayer();
181+
resetPlayer().catch((_) => {});
182182
}
183183
}}
184184
{...messageProps}

packages/uikit-react-native/src/domain/groupChannel/component/GroupChannelMessageList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
4040
if (focusAnimated) {
4141
props.onUpdateSearchItem({ startingPoint: createdAt });
4242
}
43-
props.onResetMessageListWithStartingPoint(createdAt);
43+
props.onResetMessageListWithStartingPoint(createdAt).catch((_) => {});
4444
} else {
4545
return false;
4646
}
@@ -54,7 +54,7 @@ const GroupChannelMessageList = (props: GroupChannelProps['MessageList']) => {
5454
props.onUpdateSearchItem(undefined);
5555
props.onScrolledAwayFromBottom(false);
5656

57-
await props.onResetMessageList();
57+
await props.onResetMessageList().catch((_) => {});
5858
props.onScrolledAwayFromBottom(false);
5959
lazyScrollToBottom({ animated });
6060
} else {

packages/uikit-react-native/src/domain/groupChannelSettings/component/GroupChannelSettingsMenu.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ const GroupChannelSettingsMenu = ({
4747
}
4848

4949
const toggleNotification = async () => {
50-
if (channel.myPushTriggerOption === 'off') {
51-
await channel.setMyPushTriggerOption(PushTriggerOption.DEFAULT);
52-
} else {
53-
await channel.setMyPushTriggerOption(PushTriggerOption.OFF);
50+
try {
51+
if (channel.myPushTriggerOption === 'off') {
52+
await channel.setMyPushTriggerOption(PushTriggerOption.DEFAULT);
53+
} else {
54+
await channel.setMyPushTriggerOption(PushTriggerOption.OFF);
55+
}
56+
} catch (error) {
57+
Logger.warn('Failed to toggle notification', error);
5458
}
5559
};
5660

0 commit comments

Comments
 (0)