From 4815e447fa36a2b951049ea8992c439f91f9b57a Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Mon, 30 Jan 2023 15:12:39 +0000 Subject: [PATCH] android: show alert instead of crash on user errors (#1861) * android: show alert instead of crash on user errors * show meaningful alert * update alert messages --------- Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- .../main/java/chat/simplex/app/model/SimpleXAPI.kt | 12 ++++++++++-- .../main/java/chat/simplex/app/views/WelcomeView.kt | 2 +- apps/android/app/src/main/res/values/strings.xml | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt index 820c045cb..af9acf919 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt @@ -296,6 +296,7 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a changeActiveUser_(toUserId) } catch (e: Exception) { Log.e(TAG, "Unable to set active user: ${e.stackTraceToString()}") + AlertManager.shared.showAlertMsg(generalGetString(R.string.failed_to_active_user_title), e.stackTraceToString()) } } @@ -379,11 +380,16 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a return null } - suspend fun apiCreateActiveUser(p: Profile): User { + suspend fun apiCreateActiveUser(p: Profile): User? { val r = sendCmd(CC.CreateActiveUser(p)) if (r is CR.ActiveUser) return r.user + else if (r is CR.ChatCmdError && r.chatError is ChatError.ChatErrorStore && r.chatError.storeError is StoreError.DuplicateName) { + AlertManager.shared.showAlertMsg(generalGetString(R.string.failed_to_create_user_title), generalGetString(R.string.failed_to_create_user_duplicate_desc)) + } else { + AlertManager.shared.showAlertMsg(generalGetString(R.string.failed_to_create_user_title), r.details) + } Log.d(TAG, "apiCreateActiveUser: ${r.responseType} ${r.details}") - throw Error("user not created ${r.responseType} ${r.details}") + return null } suspend fun listUsers(): List { @@ -3241,9 +3247,11 @@ sealed class StoreError { val string: String get() = when (this) { is UserContactLinkNotFound -> "userContactLinkNotFound" is GroupNotFound -> "groupNotFound" + is DuplicateName -> "duplicateName" } @Serializable @SerialName("userContactLinkNotFound") class UserContactLinkNotFound: StoreError() @Serializable @SerialName("groupNotFound") class GroupNotFound: StoreError() + @Serializable @SerialName("duplicateName") class DuplicateName: StoreError() } @Serializable diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt index a82ee0272..ec2c159a6 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt @@ -111,7 +111,7 @@ fun createProfile(chatModel: ChatModel, displayName: String, fullName: String, c withApi { val user = chatModel.controller.apiCreateActiveUser( Profile(displayName, fullName, null) - ) + ) ?: return@withApi chatModel.currentUser.value = user if (chatModel.users.isEmpty()) { chatModel.controller.startChat(user) diff --git a/apps/android/app/src/main/res/values/strings.xml b/apps/android/app/src/main/res/values/strings.xml index 881bd76ce..758fe3cec 100644 --- a/apps/android/app/src/main/res/values/strings.xml +++ b/apps/android/app/src/main/res/values/strings.xml @@ -63,6 +63,9 @@ Failed to load chat Failed to load chats Please update the app and contact developers. + Duplicate display name! + You already have a chat profile with the same display name. Please choose another name. + Error switching profile! Connection timeout