RxSwift zip operator when one observable can fail

Recently I ran into a situation where I had to make two independent API requests and one of the API requests was discardable. I was sure that Observable.zip was the way to go. So i went ahead and implemented, My code looked something like this

let request1 = usecase.request1().asObservable()
let request2 = usecase.request2().asObservable() // fetching some not so important data which are just good to have
Observable.zip(request1,request2)..observeOn(MainScheduler.instance)
   .subscribe(
      onNext: { //success code },
      onError: { _ in //error code}).disposeBy(bag:myDisposeBag)

This was apparently very easy to implement and worked well till my not-so-important request2 failed. Since I was using Observable.zip() it led to the failure of the entire operation which was not the expected behavior as per requirement. So now I had to make zip not fail even if the request2 fails but the zip should fail if request1 fails. Essentially failure of the only request1 should end up in error. The solution was surprisingly simple and easy to implement. I used catchErrorJustReturn(someValue) in request2. My implementation became something like this

let request1 = usecase.request1().asObservable()
let request2 = usecase.request2().catchErrorJustReturn([]).asObservable() // fetching some not so important data which are just good to have.Return empty array on error
Observable.zip(request1,request2)..observeOn(MainScheduler.instance)
   .subscribe(
      onNext: { //success code },
      onError: { _ in //error code}).disposeBy(bag:myDisposeBag)

So now whenever request2 fails zip gets an empty error instead of an error so the operation succeeds.

By using catchErrorJustReturn() we can prevent an observable from failing.