1
votes

So I have these two parallel calls using zip operator. I am making two network calls. I have the following questions:

  1. How can I handle the individual errors correctly
  2. If the first call fails I want to be able to exit the session but if the second network call fails I want to allow the user to still go through the session. I am seeing a 404 in my second network call in the zip and the entire chain fails with an error. I want it to be able to handle success and failure

valid session response 1: success response 2: failure

invalid session response 1: failure response 2: success

invalid session both endpoints fail

Single.zip(
api1.getData().doOnError {
    // handle error : exit right away
},
api2.getData().doOnError {
  // handle error: Set profile data to be empty but when user tries to see the profile information show error at a later point in time based oaths response
 // got 404 
},
{ response1: String, response2: CustomObject ->
    Pair(response1, response2)
}
)
.subscribeOn(Schedulers.io())
.subscribe(
    {
        handleResponse1(it.first)
       handleRespone2(it.second)
    },
    {
        Timber.d("it : $it")
       // api1 use success response: is it even possibel to get that in the iterator 
       // api 2 throwing 404 here 
    }
)
1

1 Answers

1
votes

From your question, I see that you want to continue even if one of the API fails without failing the whole chain. This can be done in the following way

  • If you want to exit on the first API call no need to handle any Error there.(You will get an error in throwable)

  • Whenever the second API fails use onErrorReturnItem to return some empty response

    Single.zip(
          api1.getData().subscribeOn(Schedulers.io()),
      api2.getData().subscribeOn(Schedulers.io())
                  .onErrorReturnItem(new Response())
      .......
    

The new Response() here is just an empty object of the response of type that you were expecting. Even if the second API fails here you will get whatever you are returing

If the first API fails here you will get a callback in Throwable or you can continue to handle error in doOnError