From 5a08a26c9a92d39c5957f04797585ffb25343fb1 Mon Sep 17 00:00:00 2001 From: Alexander Bondarenko <486682+dpwiz@users.noreply.github.com> Date: Tue, 21 Nov 2023 16:43:52 +0200 Subject: [PATCH] desktop: add exception handlers to startReceiver loop (#3417) * desktop: add exception handlers to startReceiver loop * simplify * more exceptions --------- Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Co-authored-by: Avently <7953703+avently@users.noreply.github.com> --- .../chat/simplex/common/model/SimpleXAPI.kt | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt index 85f2203e3..9298388df 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt @@ -423,8 +423,15 @@ object ChatController { receiverStarted = false break } - val msg = recvMsg(ctrl) - if (msg != null) processReceivedMsg(msg) + try { + val msg = recvMsg(ctrl) + if (msg != null) processReceivedMsg(msg) + } catch (e: Exception) { + Log.e(TAG, "ChatController recvMsg/processReceivedMsg exception: " + e.stackTraceToString()); + } catch (e: Throwable) { + Log.e(TAG, "ChatController recvMsg/processReceivedMsg throwable: " + e.stackTraceToString()) + AlertManager.shared.showAlertMsg(generalGetString(MR.strings.error), e.stackTraceToString()) + } } } } @@ -3558,7 +3565,7 @@ class APIResponse(val resp: CR, val remoteHostId: Long?, val corr: String? = nul fun decodeStr(str: String): APIResponse { return try { json.decodeFromString(str) - } catch(e: Exception) { + } catch(e: Throwable) { try { Log.d(TAG, e.localizedMessage ?: "") val data = json.parseToJsonElement(str).jsonObject @@ -3587,11 +3594,18 @@ class APIResponse(val resp: CR, val remoteHostId: Long?, val corr: String? = nul return APIResponse(CR.ChatRespError(user, ChatError.ChatErrorInvalidJSON(json.encodeToString(resp["chatError"]))), remoteHostId, corr) } } catch (e: Exception) { - Log.e(TAG, "Error while parsing chat(s): " + e.stackTraceToString()) + Log.e(TAG, "Exception while parsing chat(s): " + e.stackTraceToString()) + } catch (e: Throwable) { + Log.e(TAG, "Throwable while parsing chat(s): " + e.stackTraceToString()) + AlertManager.shared.showAlertMsg(generalGetString(MR.strings.error), e.stackTraceToString()) } APIResponse(CR.Response(type, json.encodeToString(data)), remoteHostId, corr) } catch(e: Exception) { APIResponse(CR.Invalid(str), remoteHostId = null) + } catch(e: Throwable) { + Log.e(TAG, "Throwable2 while parsing chat(s): " + e.stackTraceToString()) + AlertManager.shared.showAlertMsg(generalGetString(MR.strings.error), e.stackTraceToString()) + APIResponse(CR.Invalid(str), remoteHostId = null) } } }