How to catch error in Observable.forkJoin(...)?

38,567

Solution 1

You may catch the error in each of your observables that are being passed to forkJoin:

// Imports that support chaining of operators in older versions of RxJS
import {Observable} from 'rxjs/Observable';
import {forkJoin} from 'rxjs/add/observable/forkJoin';
import {of} from 'rxjs/add/observable/of';
import {map} from 'rxjs/add/operator/map';
import {catch} from 'rxjs/add/operator/catch';

// Code with chaining operators in older versions of RxJS
Observable.forkJoin(
  this.http.post<any[]>(URL, jsonBody1, postJson) .map((res) => res)).catch(e => Observable.of('Oops!')),
  this.http.post<any[]>(URL, jsonBody2, postJson) .map((res) => res)).catch(e => Observable.of('Oops!'))
)
.subscribe(res => this.handleResponse(res))

Also note that if you use RxJS6, you need to use catchError instead of catch, and pipe operators instead of chaining.

// Imports in RxJS6
import {forkJoin, of} from 'rxjs';
import {map, catchError} from 'rxjs/operators';

// Code with pipeable operators in RxJS6
forkJoin(
  this.http.post<any[]>(URL, jsonBody1, postJson) .pipe(map((res) => res), catchError(e => of('Oops!'))),
  this.http.post<any[]>(URL, jsonBody2, postJson) .pipe(map((res) => res), catchError(e => of('Oops!')))
)
  .subscribe(res => this.handleResponse(res))

Solution 2

This worked for me:

forkJoin(
 this.http.post<any[]>(URL, jsonBody1, postJson).pipe(catchError(error => of(error))),
 this.http.post<any[]>(URL, jsonBody2, postJson)
)
.subscribe(res => this.handleResponse(res))

The second HTTP call will be called normally, even if an error occurs on the first call

Share:
38,567

Related videos on Youtube

matchifang
Author by

matchifang

Updated on July 09, 2022

Comments

  • matchifang
    matchifang almost 2 years

    I use Observable.forkJoin() to handle the response after both HTTP calls finishes, but if either one of them returns an error, how can I catch that error?

    Observable.forkJoin(
      this.http.post<any[]>(URL, jsonBody1, postJson) .map((res) => res),
      this.http.post<any[]>(URL, jsonBody2, postJson) .map((res) => res)
    )
    .subscribe(res => this.handleResponse(res))
    
    • a better oliver
      a better oliver almost 6 years
      That depends on what you want to achieve. If one of the calls fails, then the whole observable fails and you can handle it in your subscriber.
  • matchifang
    matchifang almost 6 years
    I got an error saying that http.post().map().catch() is not a function
  • siva636
    siva636 almost 6 years
    Did you import catch?
  • Janith Widarshana
    Janith Widarshana over 5 years
    @matchifang I just can resolve that using pipe tis way http.post().pipe(map().catch())
  • epinal
    epinal about 5 years
    Do we really need the map here? I mean is it mandatory for the catchError or just an example of the pipe?
  • Yuri
    Yuri about 4 years
    You shouldn't need a map, just a catchError
  • giovannipds
    giovannipds over 3 years
    Should have mentioned catchError to avoid losing all the chain if any error occurs.
  • Anis Benna
    Anis Benna over 2 years
    I my case, I wanted to break the chain therefore this does it for me.