2

I understand the core concepts of Observable data services, but am running into trouble when I need a bit more robust service.

For a user query I have to make a chain of http calls such that, the user object is queried first and then additional data is queried after grabbing the user object. Since these extra calls depend upon the grabbed user object. With subscribers receiving the entire user object with any extra data it was able to grab.

I'm currently using userSource.mergeMap(user => extraDataNeeded(user)) to return the combined Observables. Where extraDataNeeded returns Observable.forkJoin(...OBSERVABLES) OBSERVABLES being an array of http calls that add extra data to the user object.

My issue is that when one of these extra http calls fails, the rest in the chain are cancelled. I'd like for the highest order observable created from userSource.mergeMap(user => extraDataNeeded(user)) to make all http calls irregardless if one fails. I'd like the subscribers of userSource.mergeMap(user => extraDataNeeded(user)) to receive the user object with whatever data it was able to grab, and any errors for whichever inner http calls failed. And fail entirely with no success response if the original userSource fails.

1 Answer 1

2

You can add a catch block to each of your OBSERVABLES such that a failure response simply returns an error body in the next block.

i.e.

source1.catch(e => Rx.Observable.of({error: e}));
source2.catch(e => Rx.Observable.of({error: e}));

//or made modular

function handler(e) { return Rx.Observable.of({error: e}));

var safeSource1 = source1.let(handler);
var safeSource2 = source2.let(handler);

Then when building your object you would just need to handle those errors appropriately:

Rx.Observable.forkJoin(safeSource1, safeSource2, sources => {
  //Totally made up
  var combined = {components: [], errors: []};
  for (let resp of sources) {
    if (resp.error)
      combined.errors.push(resp.error);
    else
      combined.components.push(resp);
  }
});

Errors from the userSource Observable will still be passed to the error handler and bypass the forkJoin entirely.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.