From 6c4685dce5893fdbcd2ae00bc093e55d816da7f6 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Tue, 29 Oct 2024 16:57:01 +0100 Subject: [PATCH] handle errors properly Signed-off-by: sowjanyakch --- .../conversationinfo/ConversationInfoActivity.kt | 12 ++++++------ .../talk/jobs/LeaveConversationWorker.kt | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt index b5c66e3dd..2cfcb07ea 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt @@ -21,7 +21,6 @@ import android.view.MenuItem import android.view.View import android.view.View.GONE import android.view.View.VISIBLE -import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.core.content.res.ResourcesCompat import androidx.fragment.app.FragmentTransaction @@ -660,11 +659,12 @@ class ConversationInfoActivity : startActivity(intent) } WorkInfo.State.FAILED -> { - Toast.makeText( - context, - R.string.nc_last_moderator_leaving_room_warning, - Toast.LENGTH_LONG - ).show() + val errorType = workInfo.outputData.getString("error_type") + if (errorType == LeaveConversationWorker.ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) { + Snackbar.make( binding.root, R.string.nc_last_moderator_leaving_room_warning, Snackbar.LENGTH_LONG ).show() + } else { + Snackbar.make( binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG ).show() + } } else -> { } diff --git a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt index 3c946b254..55eeb897e 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt @@ -10,6 +10,7 @@ package com.nextcloud.talk.jobs import android.annotation.SuppressLint import android.content.Context import android.util.Log +import androidx.work.Data import androidx.work.ListenableWorker import androidx.work.WorkerParameters import androidx.work.impl.utils.futures.SettableFuture @@ -28,6 +29,7 @@ import io.reactivex.Observer import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers +import retrofit2.HttpException import javax.inject.Inject @SuppressLint("RestrictedApi") @@ -67,9 +69,17 @@ class LeaveConversationWorker(context: Context, workerParams: WorkerParameters) override fun onError(e: Throwable) { Log.e(TAG, "Failed to remove self from room", e) - if (e.message?.contains("HTTP 400") == true) { - result.set(Result.failure()) + val httpException = e as? HttpException + val errorData = if (httpException?.code() == 400) { + Data.Builder() + .putString("error_type", ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) + .build() } + else { + Data.Builder() + .putString("error_type", ERROR_OTHER) + .build() } + result.set(Result.failure(errorData)) } override fun onComplete() { @@ -85,5 +95,7 @@ class LeaveConversationWorker(context: Context, workerParams: WorkerParameters) companion object { private const val TAG = "LeaveConversationWorker" + const val ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT = "NO_OTHER_MODERATORS_OR_OWNERS_LEFT" + const val ERROR_OTHER = "ERROR_OTHER" } }