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 Marcel Hibbe
parent f6489eb10d
commit 6c4685dce5
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
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.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 -> {
}

View file

@ -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"
}
}