Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS Angular2 handling 404 in Observable.forkjoin

I'm currently chaining a bunch of http requests, however I am having trouble handling 404 errors before subscribing.

My code:

in template:

...
service.getData().subscribe(
    data => this.items = data,
    err => console.log(err),
    () => console.log("Get data complete")
)
...

in service:

...
getDataUsingUrl(url) {
    return http.get(url).map(res => res.json());
}

getData() {
    return getDataUsingUrl(urlWithData).flatMap(res => {
        return Observable.forkJoin(
            // make http request for each element in res
            res.map(
                e => getDataUsingUrl(anotherUrlWithData)
            )
        )
    }).map(res => {
        // 404s from previous forkJoin
        // How can I handle the 404 errors without subscribing?

        // I am looking to make more http requests from other sources in 
        // case of a 404, but I wouldn't need to make the extra requests 
        // for the elements of res with succcessful responses

        values = doStuff(res);

        return values;
    })
}
like image 548
ray Avatar asked Feb 24 '16 03:02

ray


People also ask

What happens if observable fails in forkJoin?

The forkJoin will subscribe to the passed observables, by making the HTTP GET requests. If any input observable errors at some point, forkJoin will find the error as well and all other observables will be immediately unsubscribed.

Is RxJS forkJoin deprecated?

ForkJoin method signature has changed This is now deprecated and you must pass an array of observables to forkJoin operator.

What can I use instead of forkJoin?

concat() which will handle each observable in sequence.

Does forkJoin wait?

forkJoin will wait for all passed observables to emit and complete and then it will emit an array or an object with last values from corresponding observables.


2 Answers

I think you could use the catch operator. The callback you provide when calling it will be called when an error will occur:

getData() {
  return getDataUsingUrl(urlWithData).flatMap(res => {
    return Observable.forkJoin(
        // make http request for each element in res
        res.map(
            e => getDataUsingUrl(anotherUrlWithData)
        )
    )
  }).map(res => {
    // 404s from previous forkJoin
    // How can I handle the 404 errors without subscribing?

    // I am looking to make more http requests from other sources in 
    // case of a 404, but I wouldn't need to make the extra requests 
    // for the elements of res with succcessful responses

    values = doStuff(res);

    return values;
  })
  .catch((res) => { // <-----------
    // Handle the error
  });
}
like image 75
Thierry Templier Avatar answered Sep 19 '22 11:09

Thierry Templier


This was answered pretty well here: https://stackoverflow.com/a/38061516/628418

In short you put a catch on each observable before you hand them to forkJoin.

like image 32
Sámal Rasmussen Avatar answered Sep 21 '22 11:09

Sámal Rasmussen