Start impl UI
This commit is contained in:
@@ -6,8 +6,13 @@ import ru.fincode.tsudesk.core.network.model.NetworkError
|
||||
import ru.fincode.tsudesk.core.network.model.NetworkMeta
|
||||
import ru.fincode.tsudesk.core.network.model.NetworkResult
|
||||
import ru.fincode.tsudesk.core.network.model.NetworkResult.Success
|
||||
import java.io.EOFException
|
||||
import java.io.IOException
|
||||
import java.io.InterruptedIOException
|
||||
import java.net.ConnectException
|
||||
import java.net.NoRouteToHostException
|
||||
import java.net.SocketTimeoutException
|
||||
import java.net.UnknownHostException
|
||||
|
||||
suspend inline fun <T> apiCall(
|
||||
crossinline block: suspend () -> Response<T>
|
||||
@@ -51,8 +56,23 @@ suspend inline fun <T> apiCall(
|
||||
}
|
||||
|
||||
fun Throwable.toNetworkError(meta: NetworkMeta): NetworkError = when (this) {
|
||||
is SocketTimeoutException -> NetworkError.Timeout(meta)
|
||||
is IOException -> NetworkError.NoInternet(meta)
|
||||
|
||||
is UnknownHostException,
|
||||
is ConnectException,
|
||||
is NoRouteToHostException -> NetworkError.NoInternet(meta)
|
||||
|
||||
is SocketTimeoutException,
|
||||
is InterruptedIOException -> NetworkError.Timeout(meta)
|
||||
|
||||
is EOFException -> NetworkError.Temporary(meta)
|
||||
|
||||
is HttpException -> NetworkError.Http(code(), meta)
|
||||
|
||||
is IOException -> {
|
||||
val msg = message?.lowercase().orEmpty()
|
||||
if ("unexpected end of stream" in msg || "eof" in msg) NetworkError.Temporary(meta)
|
||||
else NetworkError.Temporary(meta) // любые IO — временные, не "NoInternet"
|
||||
}
|
||||
|
||||
else -> NetworkError.Unknown(this, meta)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,12 @@ sealed class NetworkError(open val meta: NetworkMeta) {
|
||||
data class NoInternet(override val meta: NetworkMeta) : NetworkError(meta)
|
||||
data class Timeout(override val meta: NetworkMeta) : NetworkError(meta)
|
||||
|
||||
/**
|
||||
* EOF / unexpected end of stream / reset / обрыв соединения.
|
||||
* Это НЕ NoInternet.
|
||||
*/
|
||||
data class Temporary(override val meta: NetworkMeta) : NetworkError(meta)
|
||||
|
||||
data class Http(
|
||||
val code: Int,
|
||||
override val meta: NetworkMeta
|
||||
@@ -20,7 +26,8 @@ sealed class NetworkError(open val meta: NetworkMeta) {
|
||||
fun toAppError(): AppError = when (this) {
|
||||
is NoInternet -> AppError.NoInternet
|
||||
is Timeout -> AppError.Timeout
|
||||
is Temporary -> AppError.Temporary
|
||||
is Http -> AppError.Http(code)
|
||||
is Unknown -> AppError.Unknown(throwable.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user