Updated on 2026-08-14

This commit is contained in:
Tangem 2024-07-16 13:45:48 +01:00
parent 231b70c00d
commit b716d29719
3 changed files with 31 additions and 8 deletions

View file

@ -5,6 +5,7 @@ import okio.Timeout
import retrofit2.Call import retrofit2.Call
import retrofit2.Callback import retrofit2.Callback
import retrofit2.Response import retrofit2.Response
import timber.log.Timber
internal class ApiResponseCallDelegate<T : Any>( internal class ApiResponseCallDelegate<T : Any>(
private val wrappedCall: Call<T>, private val wrappedCall: Call<T>,
@ -20,7 +21,9 @@ internal class ApiResponseCallDelegate<T : Any>(
override fun timeout(): Timeout = wrappedCall.timeout() override fun timeout(): Timeout = wrappedCall.timeout()
override fun isExecuted(): Boolean = wrappedCall.isExecuted override fun isExecuted(): Boolean = wrappedCall.isExecuted
override fun isCanceled(): Boolean = wrappedCall.isCanceled override fun isCanceled(): Boolean = wrappedCall.isCanceled
override fun cancel() { wrappedCall.cancel() } override fun cancel() {
wrappedCall.cancel()
}
private inner class ApiResponseCallback( private inner class ApiResponseCallback(
private val responseCallback: Callback<ApiResponse<T>>, private val responseCallback: Callback<ApiResponse<T>>,
@ -33,7 +36,15 @@ internal class ApiResponseCallDelegate<T : Any>(
} }
override fun onFailure(call: Call<T>, t: Throwable) { override fun onFailure(call: Call<T>, 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<T>(error) val safeResponse = apiError<T>(error)
responseCallback.onResponse(this@ApiResponseCallDelegate, Response.success(safeResponse)) responseCallback.onResponse(this@ApiResponseCallDelegate, Response.success(safeResponse))

View file

@ -73,10 +73,16 @@ sealed class ApiResponseError : Exception() {
} }
/** Represents a network error, typically when there's no connectivity. */ /** 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. */ /** 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. * Represents an unexpected exception that doesn't fall into one of the other categories.

View file

@ -2,6 +2,7 @@ package com.tangem.datasource.api.common.response
import kotlinx.coroutines.TimeoutCancellationException import kotlinx.coroutines.TimeoutCancellationException
import retrofit2.Response import retrofit2.Response
import timber.log.Timber
import java.net.ConnectException import java.net.ConnectException
import java.net.SocketTimeoutException import java.net.SocketTimeoutException
import java.net.UnknownHostException import java.net.UnknownHostException
@ -16,10 +17,15 @@ internal fun <T : Any> Response<T>.toSafeApiResponse(): ApiResponse<T> {
} else { } else {
val code = ApiResponseError.HttpException.Code.values val code = ApiResponseError.HttpException.Code.values
.firstOrNull { it.code == code() } .firstOrNull { it.code == code() }
val e = if (code == null) { val e = try {
ApiResponseError.UnknownException(IllegalArgumentException("Unknown error status code: ${code()}")) if (code == null) {
} else { ApiResponseError.UnknownException(IllegalArgumentException("Unknown error status code: ${code()}"))
ApiResponseError.HttpException(code, message(), errorBody()?.string()) } else {
ApiResponseError.HttpException(code, message(), errorBody()?.string())
}
} catch (e: Exception) {
Timber.e(e, "UnknownException occured")
ApiResponseError.UnknownException(e)
} }
apiError(e) apiError(e)