From 360dcbb3ee6ce97b745cd6c54a47905a26c2dc06 Mon Sep 17 00:00:00 2001 From: kroegerama <1519044+kroegerama@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:49:59 +0100 Subject: [PATCH] add `ThrowableCallError` / use in `PageSizeDataSource` --- .../kaiteki/retrofit/arrow/CallError.kt | 15 +++++++++++ .../retrofit/datasource/PageSizeDataSource.kt | 26 ++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/retrofit/src/main/kotlin/com/kroegerama/kaiteki/retrofit/arrow/CallError.kt b/retrofit/src/main/kotlin/com/kroegerama/kaiteki/retrofit/arrow/CallError.kt index c629492..9d709dc 100644 --- a/retrofit/src/main/kotlin/com/kroegerama/kaiteki/retrofit/arrow/CallError.kt +++ b/retrofit/src/main/kotlin/com/kroegerama/kaiteki/retrofit/arrow/CallError.kt @@ -25,3 +25,18 @@ data class IOError( data class UnexpectedError( val cause: Throwable ) : CallError + +data class ThrowableCallError( + val delegate: CallError +) : Throwable( + message = when (delegate) { + is HttpError -> "HTTP ${delegate.code}" + is IOError -> delegate.cause.message + is UnexpectedError -> delegate.cause.message + }, + cause = when (delegate) { + is HttpError -> null + is IOError -> delegate.cause + is UnexpectedError -> delegate.cause + } +) diff --git a/retrofit/src/main/kotlin/com/kroegerama/kaiteki/retrofit/datasource/PageSizeDataSource.kt b/retrofit/src/main/kotlin/com/kroegerama/kaiteki/retrofit/datasource/PageSizeDataSource.kt index 7654925..d072cdf 100644 --- a/retrofit/src/main/kotlin/com/kroegerama/kaiteki/retrofit/datasource/PageSizeDataSource.kt +++ b/retrofit/src/main/kotlin/com/kroegerama/kaiteki/retrofit/datasource/PageSizeDataSource.kt @@ -10,9 +10,13 @@ import androidx.paging.PagingState import androidx.paging.cachedIn import com.kroegerama.kaiteki.flow.UpdatableFlow import com.kroegerama.kaiteki.flow.updatable +import com.kroegerama.kaiteki.retrofit.arrow.HttpError +import com.kroegerama.kaiteki.retrofit.arrow.IOError +import com.kroegerama.kaiteki.retrofit.arrow.ThrowableCallError +import com.kroegerama.kaiteki.retrofit.arrow.UnexpectedError import kotlinx.coroutines.CancellationException import retrofit2.Response -import timber.log.Timber +import java.io.IOException abstract class PageSizeDataSource : PagingSource() { @@ -29,7 +33,15 @@ abstract class PageSizeDataSource : PagingSource() { val response = makeCall(page, size) if (!response.isSuccessful) { - LoadResult.Error(IllegalStateException("Response code was ${response.code()}")) + LoadResult.Error( + ThrowableCallError( + HttpError( + code = response.code(), + message = response.message(), + body = response.errorBody() + ) + ) + ) } else { val responseBody = response.body()!! val data = responseBody.extractData() @@ -44,8 +56,14 @@ abstract class PageSizeDataSource : PagingSource() { if (e is CancellationException) { throw e } - Timber.w(e) - LoadResult.Error(e) + LoadResult.Error( + ThrowableCallError( + when (e) { + is IOException -> IOError(e) + else -> UnexpectedError(e) + } + ) + ) } abstract suspend fun makeCall(page: Int, size: Int): Response