diff --git a/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ApiResponseCallDelegate.kt b/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ApiResponseCallDelegate.kt index 52a2e4cc6f..919be8e848 100644 --- a/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ApiResponseCallDelegate.kt +++ b/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ApiResponseCallDelegate.kt @@ -5,6 +5,7 @@ import okio.Timeout import retrofit2.Call import retrofit2.Callback import retrofit2.Response +import timber.log.Timber internal class ApiResponseCallDelegate( private val wrappedCall: Call, @@ -20,7 +21,9 @@ internal class ApiResponseCallDelegate( override fun timeout(): Timeout = wrappedCall.timeout() override fun isExecuted(): Boolean = wrappedCall.isExecuted override fun isCanceled(): Boolean = wrappedCall.isCanceled - override fun cancel() { wrappedCall.cancel() } + override fun cancel() { + wrappedCall.cancel() + } private inner class ApiResponseCallback( private val responseCallback: Callback>, @@ -33,7 +36,15 @@ internal class ApiResponseCallDelegate( } override fun onFailure(call: Call, t: Throwable) { - val error = t.toApiError() + val error = try { + t.toApiError() + } catch (e: ApiResponseError) { + Timber.e(e, "error map toApiError") + e + } catch (e: Exception) { + Timber.e(e, "onFailure UnknownException") + ApiResponseError.UnknownException(e) + } val safeResponse = apiError(error) responseCallback.onResponse(this@ApiResponseCallDelegate, Response.success(safeResponse)) diff --git a/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ApiResponseError.kt b/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ApiResponseError.kt index cfd2146441..22162b76b4 100644 --- a/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ApiResponseError.kt +++ b/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ApiResponseError.kt @@ -73,10 +73,16 @@ sealed class ApiResponseError : Exception() { } /** Represents a network error, typically when there's no connectivity. */ - data object NetworkException : ApiResponseError() + @Suppress("UnusedPrivateMember") + data object NetworkException : ApiResponseError() { + private fun readResolve(): Any = NetworkException + } /** Represents a timeout error, typically when the server takes too long to respond. */ - data object TimeoutException : ApiResponseError() + @Suppress("UnusedPrivateMember") + data object TimeoutException : ApiResponseError() { + private fun readResolve(): Any = TimeoutException + } /** * Represents an unexpected exception that doesn't fall into one of the other categories. diff --git a/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ResponseExt.kt b/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ResponseExt.kt index 92a5af0ac5..f696275bc3 100644 --- a/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ResponseExt.kt +++ b/core/datasource/src/main/java/com/tangem/datasource/api/common/response/ResponseExt.kt @@ -2,6 +2,7 @@ package com.tangem.datasource.api.common.response import kotlinx.coroutines.TimeoutCancellationException import retrofit2.Response +import timber.log.Timber import java.net.ConnectException import java.net.SocketTimeoutException import java.net.UnknownHostException @@ -16,10 +17,15 @@ internal fun Response.toSafeApiResponse(): ApiResponse { } else { val code = ApiResponseError.HttpException.Code.values .firstOrNull { it.code == code() } - val e = if (code == null) { - ApiResponseError.UnknownException(IllegalArgumentException("Unknown error status code: ${code()}")) - } else { - ApiResponseError.HttpException(code, message(), errorBody()?.string()) + val e = try { + if (code == null) { + ApiResponseError.UnknownException(IllegalArgumentException("Unknown error status code: ${code()}")) + } else { + ApiResponseError.HttpException(code, message(), errorBody()?.string()) + } + } catch (e: Exception) { + Timber.e(e, "UnknownException occured") + ApiResponseError.UnknownException(e) } apiError(e)