handle errors properly

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-10-29 16:57:01 +01:00 committed by backportbot[bot]
parent 9028ad0d1c
commit bbcc3fb0a0
2 changed files with 20 additions and 8 deletions

View file

@ -21,7 +21,6 @@ import android.view.MenuItem
import android.view.View import android.view.View
import android.view.View.GONE import android.view.View.GONE
import android.view.View.VISIBLE import android.view.View.VISIBLE
import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentTransaction import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
@ -657,11 +656,12 @@ class ConversationInfoActivity :
startActivity(intent) startActivity(intent)
} }
WorkInfo.State.FAILED -> { WorkInfo.State.FAILED -> {
Toast.makeText( val errorType = workInfo.outputData.getString("error_type")
context, if (errorType == LeaveConversationWorker.ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) {
R.string.nc_last_moderator_leaving_room_warning, Snackbar.make( binding.root, R.string.nc_last_moderator_leaving_room_warning, Snackbar.LENGTH_LONG ).show()
Toast.LENGTH_LONG } else {
).show() Snackbar.make( binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG ).show()
}
} }
else -> { else -> {
} }

View file

@ -10,6 +10,7 @@ package com.nextcloud.talk.jobs
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import androidx.work.Data
import androidx.work.ListenableWorker import androidx.work.ListenableWorker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import androidx.work.impl.utils.futures.SettableFuture import androidx.work.impl.utils.futures.SettableFuture
@ -28,6 +29,7 @@ import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException
import javax.inject.Inject import javax.inject.Inject
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
@ -67,9 +69,17 @@ class LeaveConversationWorker(context: Context, workerParams: WorkerParameters)
override fun onError(e: Throwable) { override fun onError(e: Throwable) {
Log.e(TAG, "Failed to remove self from room", e) Log.e(TAG, "Failed to remove self from room", e)
if (e.message?.contains("HTTP 400") == true) { val httpException = e as? HttpException
result.set(Result.failure()) 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() { override fun onComplete() {
@ -85,5 +95,7 @@ class LeaveConversationWorker(context: Context, workerParams: WorkerParameters)
companion object { companion object {
private const val TAG = "LeaveConversationWorker" 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"
} }
} }