rizmaulana / kotlin-mvvm-covid19

This repository contains simple COVID19 data monitoring with android stack MVVM, Live Data, Koin, RxJava, RxBinding, Offline first with simple caching, etc
Apache License 2.0
435 stars 111 forks source link

How to use the responseToResult method #60

Open tcqq opened 4 years ago

tcqq commented 4 years ago

@rizmaulana Hello, if need to display a prompt when there is no network connection or the request times out, how can I use responseToResult to make all network requests display prompt information under these circumstances?

fun <T> Observable<T>.responseToResult(): Observable<Result<T>> {
    return map { it.asResult() }
        .onErrorReturn {
            when (it) {
                is HttpException,
                is SocketTimeoutException,
                is UnknownHostException -> {
                    it.asErrorResult()
                }
                else -> throw it
            }
        }
}