Kulwinder Singh
08/16/2019, 6:08 AMprivate val realInterceptor = Interceptor { chain ->
if (!NetworkConnection.getInstance().isConnected) {
throw NoInternetException()
}
/..../
}
but to work with this i have to add try/catch
block on every request, there are so many requests. Below is sample of how i'm handling each network request->
val response = apiService.getItems().toNetworkResult()
when (response) {
is NetworkResult.Success -> onDataLoaded(response.data)
is NetworkResult.Error -> toast(response.errorCode)
}
and this is how toNetworkResult
looks like
fun <T : Any> Response<T>.toNetworkResult(): NetworkResult<T> {
val response = this
if (response.isSuccessful)
return NetworkResult.Success(response.body()!!)
val error = GsonUtils.toObject<ErrorResponse>(response.errorBody()!!.string())
return NetworkResult.Error(response.code(), error)
}
I just want to know if it is possible to handle it inside toNetworkResult()
or any other similar solution i can implement, where i don't have to add try/catch
on every requestgildor
08/16/2019, 6:16 AMif (!NetworkConnection.getInstance().isConnected) {
throw NoInternetException()
}
gildor
08/16/2019, 6:16 AMKulwinder Singh
08/16/2019, 6:17 AMgildor
08/16/2019, 6:18 AMgildor
08/16/2019, 6:19 AMlouiscad
08/16/2019, 6:19 AMIOException
subclass thrown.gildor
08/16/2019, 6:20 AMgildor
08/16/2019, 6:22 AMfun Throwable.isNotConnected() = this is UnknownHostException || cause is UnknownHostException
fun Throwable.isTimeout() = this is SocketTimeoutException || cause is SocketTimeoutException
but it used only to handle it in UI, we never change interceptors or request logicgildor
08/16/2019, 6:24 AMisConnected
doesn’t handle anything usefulgildor
08/16/2019, 6:27 AMKulwinder Singh
08/16/2019, 6:36 AMtry/catch
boilerplate i have created this high order function and it worked. let me know if it is ok ?
inline fun withNetworkCheck(call: () -> Unit) {
try {
call()
} catch (e: Exception) {
if (e.isNotConnected()) {
toast("Not connected to internet")
} else if (e.isTimeout()) {
toast("Network connection timeout")
}
}
}
fun Throwable.isNotConnected() = this is UnknownHostException || cause is UnknownHostException
fun Throwable.isTimeout() = this is SocketTimeoutException || cause is SocketTimeoutException
Kulwinder Singh
08/16/2019, 6:37 AMwithNetworkCheck {
val response = apiService.getItems().toNetworkResult()
when (response) {
is NetworkResult.Success -> onDataLoaded(response.data)
is NetworkResult.Error -> toast(response.errorCode)
}
}
Kulwinder Singh
08/16/2019, 6:46 AMahulyk
08/16/2019, 7:42 AMabstract class BaseUseCase<out T, in P> where T : Any {
abstract suspend fun run(params: P): Result<Failure, T>
operator fun invoke(
scope: CoroutineScope,
params: P,
onResult: (Result<Failure, T>) -> Unit = {}
) {
scope.launch {
try {
withContext(IO) { run(params) }
} catch (e: Exception) {
mapFailure(e)
}
.run {
onResult(this)
}
}
}
private fun mapFailure(e: Exception) =
when (e) {
is HttpException -> Failure.ServerError(e)
is UnknownHostException, is SocketTimeoutException -> Failure.NetworkConnection(e)
else -> Failure.FeatureFailure(e)
}
.run {
Timber.w(e)
Result.Failure(this)
}
}
Kulwinder Singh
08/16/2019, 2:47 PMSaran Akkaraviwat
08/22/2019, 4:34 AM