Skip to content

Commit 6f2a480

Browse files
committed
Add RxJava error handler
1 parent 8a3b734 commit 6f2a480

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

base/src/main/java/com/enginebai/base/di/UtilsModule.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.enginebai.base.di
22

33
import com.enginebai.base.BuildConfig
4+
import com.enginebai.base.utils.RxErrorHandler
45
import com.enginebai.base.utils.logging.TimberLoggerDebugTree
56
import com.google.gson.Gson
67
import com.orhanobut.logger.AndroidLogAdapter
78
import com.orhanobut.logger.FormatStrategy
89
import com.orhanobut.logger.PrettyFormatStrategy
10+
import org.koin.android.ext.koin.androidApplication
911
import org.koin.dsl.module
1012
import timber.log.Timber
1113

@@ -31,4 +33,8 @@ val loggingModule = module {
3133

3234
val gsonModule = module {
3335
single { Gson() }
36+
}
37+
38+
val errorHandleModule = module {
39+
single(createdAtStart = true) { RxErrorHandler(androidApplication()) }
3440
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.enginebai.base.utils
2+
3+
import android.app.Application
4+
import com.enginebai.base.R
5+
import io.reactivex.exceptions.OnErrorNotImplementedException
6+
import io.reactivex.exceptions.UndeliverableException
7+
import io.reactivex.functions.Consumer
8+
import io.reactivex.plugins.RxJavaPlugins
9+
import io.reactivex.subjects.PublishSubject
10+
import retrofit2.HttpException
11+
import timber.log.Timber
12+
import java.net.ConnectException
13+
import java.net.SocketException
14+
import java.net.SocketTimeoutException
15+
import java.net.UnknownHostException
16+
17+
class RxErrorHandler(private val application: Application) : Consumer<Throwable> {
18+
19+
val errorMessageToDisplay = PublishSubject.create<String>()
20+
21+
init {
22+
RxJavaPlugins.setErrorHandler(this)
23+
}
24+
25+
override fun accept(t: Throwable) {
26+
when (val cause = parseCause(t)) {
27+
is SocketTimeoutException, is ConnectException, is UnknownHostException, is SocketException -> {
28+
Timber.w("Network fail: $cause")
29+
errorMessageToDisplay.onNext(application.getString(R.string.error_network_fail))
30+
}
31+
is HttpException -> {
32+
val url = cause.response()?.raw()?.request?.url
33+
errorMessageToDisplay.onNext("HTTP ${cause.code()} of $url")
34+
}
35+
else -> {
36+
Timber.e(cause)
37+
throw cause
38+
}
39+
}
40+
}
41+
42+
private fun parseCause(t: Throwable): Throwable {
43+
when (t) {
44+
is OnErrorNotImplementedException, is UndeliverableException, is RuntimeException -> {
45+
t.cause?.run { return this } ?: throw ParseCauseFailException(t)
46+
}
47+
}
48+
return t
49+
}
50+
}
51+
52+
class ParseCauseFailException(t: Throwable) : RuntimeException(t)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="error_network_fail">Looks like your are offline.</string>
4+
</resources>

0 commit comments

Comments
 (0)