Skip to content

Commit 127e166

Browse files
committed
fix(contextchat): catch network failures when loading chat message context
ConnectException (and other IOExceptions) thrown from the uncaught network call inside viewModelScope.launch had no propagation path, crashing the app via the last-resort uncaught exception handler instead of surfacing the existing Error UI state. Assisted-by: Claude Code:claude-sonnet-5 Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
1 parent 4bed50a commit 127e166

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

app/src/main/java/com/nextcloud/talk/contextchat/ContextChatViewModel.kt

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package com.nextcloud.talk.contextchat
99

10+
import android.util.Log
1011
import androidx.lifecycle.ViewModel
1112
import androidx.lifecycle.viewModelScope
1213
import autodagger.AutoInjector
@@ -50,32 +51,37 @@ class ContextChatViewModel @Inject constructor(private val chatNetworkDataSource
5051
_getContextChatMessagesState.value = ContextChatRetrieveUiState.Error
5152
}
5253

53-
var messages = chatNetworkDataSource.getContextForChatMessage(
54-
credentials = credentials,
55-
baseUrl = baseUrl,
56-
token = token,
57-
messageId = messageId,
58-
limit = LIMIT,
59-
threadId = threadId?.toIntOrNull()
60-
)
54+
try {
55+
var messages = chatNetworkDataSource.getContextForChatMessage(
56+
credentials = credentials,
57+
baseUrl = baseUrl,
58+
token = token,
59+
messageId = messageId,
60+
limit = LIMIT,
61+
threadId = threadId?.toIntOrNull()
62+
)
6163

62-
if (threadId.isNullOrEmpty()) {
63-
messages = messages.filter { !isThreadChildMessage(it) }
64-
}
64+
if (threadId.isNullOrEmpty()) {
65+
messages = messages.filter { !isThreadChildMessage(it) }
66+
}
6567

66-
val subTitle = if (threadId?.isNotEmpty() == true) {
67-
messages.firstOrNull()?.threadTitle
68-
} else {
69-
""
70-
}
68+
val subTitle = if (threadId?.isNotEmpty() == true) {
69+
messages.firstOrNull()?.threadTitle
70+
} else {
71+
""
72+
}
7173

72-
_getContextChatMessagesState.value = ContextChatRetrieveUiState.Success(
73-
messageId = messageId,
74-
threadId = threadId,
75-
messages = messages,
76-
title = title,
77-
subTitle = subTitle
78-
)
74+
_getContextChatMessagesState.value = ContextChatRetrieveUiState.Success(
75+
messageId = messageId,
76+
threadId = threadId,
77+
messages = messages,
78+
title = title,
79+
subTitle = subTitle
80+
)
81+
} catch (e: Exception) {
82+
Log.e(TAG, "Failed to load chat message context", e)
83+
_getContextChatMessagesState.value = ContextChatRetrieveUiState.Error
84+
}
7985
}
8086
}
8187

@@ -96,6 +102,7 @@ class ContextChatViewModel @Inject constructor(private val chatNetworkDataSource
96102
}
97103

98104
companion object {
105+
private val TAG = ContextChatViewModel::class.java.simpleName
99106
private const val LIMIT = 50
100107
}
101108
}

0 commit comments

Comments
 (0)