don't crash on unexpected json responses (#3635)

This commit is contained in:
Levi Bard 2023-09-05 09:34:20 +02:00 committed by GitHub
commit 7016fa3abc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 2 deletions

View File

@ -332,7 +332,7 @@ class AccountListFragment :
val linkHeader = response.headers()["Link"]
onFetchAccountsSuccess(accountList, linkHeader)
} catch (exception: IOException) {
} catch (exception: Exception) {
onFetchAccountsFailure(exception)
}
}

View File

@ -1,9 +1,10 @@
package com.keylesspalace.tusky.components.timeline.util
import com.google.gson.JsonParseException
import retrofit2.HttpException
import java.io.IOException
fun Throwable.isExpected() = this is IOException || this is HttpException
fun Throwable.isExpected() = this is IOException || this is HttpException || this is JsonParseException
inline fun <T> ifExpected(
t: Throwable,

View File

@ -15,6 +15,7 @@
package com.keylesspalace.tusky.components.timeline.viewmodel
import android.util.Log
import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadType
import androidx.paging.PagingState
@ -117,6 +118,7 @@ class CachedTimelineRemoteMediator(
return MediatorResult.Success(endOfPaginationReached = statuses.isEmpty())
} catch (e: Exception) {
return ifExpected(e) {
Log.w(TAG, "Failed to load timeline", e)
MediatorResult.Error(e)
}
}
@ -175,4 +177,8 @@ class CachedTimelineRemoteMediator(
}
return overlappedStatuses
}
companion object {
private const val TAG = "CachedTimelineRM"
}
}

View File

@ -15,6 +15,7 @@
package com.keylesspalace.tusky.components.timeline.viewmodel
import android.util.Log
import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadType
import androidx.paging.PagingState
@ -106,8 +107,13 @@ class NetworkTimelineRemoteMediator(
return MediatorResult.Success(endOfPaginationReached = statuses.isEmpty())
} catch (e: Exception) {
return ifExpected(e) {
Log.w(TAG, "Failed to load timeline", e)
MediatorResult.Error(e)
}
}
}
companion object {
private const val TAG = "NetworkTimelineRM"
}
}