fix: Improve error handling when blocking accounts (#131)

The previous code ran the API call in a `try/catch block`, and handled
errors in the `catch`. But `NetworkResult` already catches the exception
and transforms it to a failure, so the error case was not handled.

Replace with `NetworkResult.fold`.
This commit is contained in:
Nik Clayton 2023-09-27 18:32:58 +02:00 committed by GitHub
parent 50d9aedad9
commit b947c1b289
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 11 deletions

View File

@ -210,16 +210,15 @@ class AccountListFragment :
override fun onBlock(block: Boolean, id: String, position: Int) { override fun onBlock(block: Boolean, id: String, position: Int) {
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
try { if (block) {
if (!block) {
api.unblockAccount(id)
} else {
api.blockAccount(id) api.blockAccount(id)
} } else {
api.unblockAccount(id)
}.fold({
onBlockSuccess(block, id, position) onBlockSuccess(block, id, position)
} catch (_: Throwable) { }, {
onBlockFailure(block, id) onBlockFailure(block, id, it)
} },)
} }
} }
@ -240,13 +239,13 @@ class AccountListFragment :
} }
} }
private fun onBlockFailure(block: Boolean, accountId: String) { private fun onBlockFailure(block: Boolean, accountId: String, throwable: Throwable) {
val verb = if (block) { val verb = if (block) {
"block" "block"
} else { } else {
"unblock" "unblock"
} }
Log.e(TAG, "Failed to $verb account accountId $accountId") Log.e(TAG, "Failed to $verb account accountId $accountId: $throwable")
} }
override fun onRespondToFollowRequest( override fun onRespondToFollowRequest(