Home > Enterprise >  How to handle Exception in map() RxJava
How to handle Exception in map() RxJava

Time:02-16

I'm calling an API which throws 404 status code and due to that app gets crashed. The code snippet is mentioned below:

fun loadAndSavedata(
    type: Type,
    bookmark: String? = null,
): Single<MetaData> {
    return loadFilesByType(type, bookmark)
        .map { response ->
            if (!response.isSuccessfulAndHasBody()) {
                //This line gets pointed as cause
                throw OfflineModeException(response)
            }
                response.body()!!

        }
        .flatMap { loadNextPages(it, type) }
}

The error, I receive in logcat is:

Process: com.example.app, PID: 23797
io.reactivex.rxjava3.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | com.com.example.app.models.OfflineModeException: HTTP 404 
    at io.reactivex.rxjava3.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
    at io.reactivex.rxjava3.internal.operators.single.SingleZipArray$ZipCoordinator.innerError(SingleZipArray.java:139)
    at io.reactivex.rxjava3.internal.operators.single.SingleZipArray$ZipSingleObserver.onError(SingleZipArray.java:175)
    at io.reactivex.rxjava3.internal.operators.single.SingleFlatMap$SingleFlatMapCallback.onError(SingleFlatMap.java:91)
    at io.reactivex.rxjava3.internal.operators.single.SingleFlatMap$SingleFlatMapCallback.onError(SingleFlatMap.java:91)
    at io.reactivex.rxjava3.internal.operators.single.SingleFlatMap$SingleFlatMapCallback.onError(SingleFlatMap.java:91)
    at io.reactivex.rxjava3.internal.operators.single.SingleFlatMap$SingleFlatMapCallback.onError(SingleFlatMap.java:91)
    at io.reactivex.rxjava3.internal.operators.single.SingleMap$MapSingleObserver.onError(SingleMap.java:70)
    at io.reactivex.rxjava3.internal.operators.single.SingleMap$MapSingleObserver.onSuccess(SingleMap.java:61)
    at io.reactivex.rxjava3.internal.operators.observable.ObservableSingleSingle$SingleElementObserver.onComplete(ObservableSingleSingle.java:110)
    at io.reactivex.rxjava3.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.onComplete(ObservableSubscribeOn.java:68)
    at retrofit2.adapter.rxjava3.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:52)
    at io.reactivex.rxjava3.core.Observable.subscribe(Observable.java:13095)
    at io.reactivex.rxjava3.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
    at io.reactivex.rxjava3.core.Scheduler$DisposeTask.run(Scheduler.java:589)
    at io.reactivex.rxjava3.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:65)
    at io.reactivex.rxjava3.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:56)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:920)
 Caused by: com.com.example.app.models.OfflineModeException: HTTP 404 

Can anyone please guide me how I can handle this error and avoid crash?

CodePudding user response:

throw OfflineModeException(response) // you are explicitly throwing exception on unsuccessful response. Remove this line or add a try catch block

CodePudding user response:

You can not use throw directly because map function accept functional Interface Function<T,R>. In Function<T,R> abstract method is R apply(T t) so in apply function no throws present so you can not use throw keyword without try catch block in map.

  • Related