14

I am querying a single API endpoint multiple times except with different parameters. For what ever reason some of these requests may fail and return a 500 error. If they do i still want the other requests to carry on and return me the data of all the successfull requests.

let terms = [];
terms.push(this.category.category);
terms = terms.concat(this.category.interests.map((x) => x.category));

for (let i = 0; i < terms.length; i++) {

    const params = {
        term: terms[i],
        mode: 'ByInterest'
    };


    const request = this.evidenceService.get(this.job.job_id, params).map((res) => res.interactions);

    this.requests.push(request);

}

const combined = Observable.forkJoin(this.requests);

combined.subscribe((res) => {
    this.interactions = res;
});

2 Answers 2

11

Most easily chain each request with catch that emits just null:

const request = this.evidenceService.get(...)
  .map(...)
  .catch(error => Observable.of(null)); // Or whatever you want here

The failed requests will have just null value in the resulting array that will be emitted by forkJoin.

Note that you can't use Observable.empty() in this situation because empty() doesn't emit anything and just completes while forkJoin requires all source Observables to emit at least one value.

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

1 Comment

See "catchError" in other answer stackoverflow.com/a/50130670/745931 for the "newer" rxjs version, where you have to use pipe() instead of chaining...
5

You could use rxjs catchError :

const request = this.evidenceService.get(this.job.job_id, params)
.pipe(map((res) => res.interactions),
catchError(error => of(undefined)));

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.