Handling Error RXJava Android with Kotlin
Asked Answered
R

1

19

Hi I'm new with RxJava and Kotlin and I loose some concepts about it.

I have "api" like this:

interface VehiclesService {
    @GET("/vehicles/")
    fun getVehicles(): Single<List<Vehicle>>
}

Then I create the retrofit client, etc.. like this:

var retrofit = RetrofitClient().getInstance()
vehiclesAPI = retrofit!!.create(VehiclesService ::class.java)

finally I do the call:

private fun fetchData() {
        compositeDisposable.add(vehiclesAPI .getVehicles()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { vehicles -> displayData(vehicles) }
        )
    }

And here is where I have the error when I try to launch:

The exception was not handled due to missing onError handler in the subscribe() method call

I know that the error is quite explicit. So I know what is missing, but what I don't know is HOW to handle this error.

I tried adding : .doOnError { error -> Log.d("MainClass",error.message) } but still telling same error message.

Recalesce answered 23/9, 2018 at 17:43 Comment(0)
B
35

You can pass another lambda to subscribe to handle the errors for a specific stream like this:

    private fun fetchData() {
    compositeDisposable.add(vehiclesAPI .getVehicles()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe( { vehicles -> displayData(vehicles) }, { throwable -> //handle error } )
    )
}

P.S: doOnError and other Side Effect operators, will not affect the stream in anyway, they just anticipate the values emitted for side-effect operations like logging for example.

Batangas answered 23/9, 2018 at 17:57 Comment(8)
If I separetd it by comma, as you posted, I'm getting an error: `unexpected tokens (use ; to separate expression in same line) ´ . If I put this semicolon, I get an error to.Recalesce
My bad, I didn't see you were using a lambda for subscribe, edited.Batangas
OH! Perfect !! Now I see how is coded, I understang whats going on, and how to do it. Thanks!Recalesce
Glad to be of helpBatangas
Why it is crashing the app though I am still confused. is it necessary to handle the error in subscribe?Demars
@Dr.aNdRO can you clarify your case so that I can help? and answering your question, sometimes there are cases when your app might crash even though you've added an onError block in the subscription site, see here: github.com/ReactiveX/RxJava/blob/2.x/…Batangas
So we need a try catch block? Rx supposed to handle the exception and catch it and supply that in onError in subscribe right? If I Don't write onError in subscribe My app is crashing if Server throws 500 exception. Why?Demars
Thanks for the link ahmed I think this[OnErrorNotImplementedException: reintroduced to detect when the user forgot to add error handling to subscribe()] might be the exception in my case I will check it outDemars

© 2022 - 2024 — McMap. All rights reserved.