Created
July 28, 2019 12:37
-
-
Save NayaneshGupte/e9dbc281ecd6c9828c394f9d553ecfe3 to your computer and use it in GitHub Desktop.
Error Handling Using RxJava
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.test.myapplication | |
| import android.util.Log | |
| import io.reactivex.Observable | |
| import io.reactivex.Observer | |
| import io.reactivex.disposables.CompositeDisposable | |
| import io.reactivex.disposables.Disposable | |
| class RxKotlinErrorHandling { | |
| private val compositeDisposable = CompositeDisposable() | |
| /** | |
| * Output : | |
| * | |
| * OnError ------- | |
| * onNextPrintValue 1 | |
| * onNextPrintValue 2 | |
| * onErrorPrintError Exception on 3 | |
| */ | |
| fun demoOnError(): Disposable { | |
| Log.d("RxKotlinErrorHandling", "OnError -------") | |
| return Observable.just(1, 2, 3, 4) | |
| .map { t: Int -> doSomethingThatThrowsException(t) } | |
| .subscribe(this::onNextPrintValue, this::onErrorPrintError, this::onComplete) | |
| } | |
| /** | |
| * Output: | |
| * | |
| * onExceptionResumeNext ------- | |
| * onNextPrintValue 1 | |
| * onNextPrintValue 2 | |
| * doSomethingElse | |
| * onNextPrintValue 5 | |
| * OnError ------- | |
| * onNextPrintValue 1 | |
| * onNextPrintValue 2 | |
| * onErrorPrintError Exception on 3 | |
| */ | |
| fun demoOnExceptionResumeNext() { | |
| Log.d("RxKotlinErrorHandling", "onExceptionResumeNext -------") | |
| val disposable = Observable.just(1, 2, 3, 4) | |
| .doOnNext { doSomethingThatThrowsException(it) } | |
| .onExceptionResumeNext { doSomethingElse(it) } | |
| .subscribe(this::onNextPrintValue, this::onErrorPrintError, this::onComplete) | |
| compositeDisposable.add(disposable) | |
| } | |
| /** | |
| * output: | |
| * | |
| * doOnError ------- | |
| * onNextPrintValue 1 | |
| * onNextPrintValue 2 | |
| * onErrorPrintError Exception on 3 | |
| * onErrorPrintError Exception on 3 | |
| */ | |
| fun demoDoOnError() { | |
| Log.d("RxKotlinErrorHandling", "doOnError -------") | |
| val disposable = Observable.fromArray(1, 2, 3, 4) | |
| .doOnNext { doSomethingThatThrowsException(it) } | |
| .doOnError { onErrorPrintError(it) } | |
| .subscribe(this::onNextPrintValue, this::onErrorPrintError, this::onComplete) | |
| compositeDisposable.add(disposable) | |
| } | |
| /** | |
| * output: | |
| * | |
| * onErrorReturnItem ------- | |
| * onNextPrintValue 1 | |
| * onNextPrintValue 2 | |
| * onNextPrintValue -1 | |
| * onComplete | |
| */ | |
| fun demoOnErrorReturnItem() { | |
| Log.d("RxKotlinErrorHandling", "onErrorReturnItem -------") | |
| val disposable = Observable.fromArray(1, 2, 3, 4) | |
| .doOnNext { doSomethingThatThrowsException(it) } | |
| .onErrorReturnItem(returnItem()) | |
| .subscribe(this::onNextPrintValue, this::onErrorPrintError, this::onComplete) | |
| compositeDisposable.add(disposable) | |
| } | |
| /** | |
| * output : | |
| * | |
| * onErrorReturn ------- | |
| * onNextPrintValue 1 | |
| * onNextPrintValue 2 | |
| * onErrorReturnValue Exception on 3 | |
| * onNextPrintValue -1 | |
| * onComplete | |
| */ | |
| fun demoOnErrorReturn() { | |
| Log.d("RxKotlinErrorHandling", "onErrorReturn -------") | |
| val disposable = Observable.fromArray(1, 2, 3, 4) | |
| .doOnNext { doSomethingThatThrowsException(it) } | |
| .onErrorReturn(this::onErrorReturnValue) | |
| .subscribe(this::onNextPrintValue, this::onErrorPrintError, this::onComplete) | |
| compositeDisposable.add(disposable) | |
| } | |
| private fun doSomethingThatThrowsException(it: Int): Int { | |
| when (it) { | |
| 3 -> throw RuntimeException("Exception on $it") | |
| else -> return it | |
| } | |
| } | |
| private fun returnItem(): Int { | |
| return -1 | |
| } | |
| private fun onNextPrintValue(i: Int?) { | |
| Log.d("RxKotlinErrorHandling", "onNextPrintValue $i") | |
| } | |
| private fun onErrorPrintError(e: Throwable?) { | |
| Log.d("RxKotlinErrorHandling", "onErrorPrintError " + e?.localizedMessage) | |
| } | |
| private fun onErrorReturnValue(e: Throwable?): Int { | |
| Log.d("RxKotlinErrorHandling", "onErrorReturnValue " + e?.localizedMessage) | |
| return when (e) { | |
| is RuntimeException -> returnItem() | |
| else -> 100 | |
| } | |
| } | |
| private fun doSomethingElse(it: Observer<in Int>) { | |
| Log.d("RxKotlinErrorHandling", "doSomethingElse") | |
| it.onNext(5) | |
| it.onSubscribe(demoOnError()) | |
| } | |
| private fun onComplete() { | |
| Log.d("RxKotlinErrorHandling", "onComplete") | |
| } | |
| fun onCleared() { | |
| compositeDisposable.clear() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment