Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)) 
like image 620
matchifang Avatar asked Jun 29 '18 14:06

matchifang


People also ask

How do you catch errors 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. Ensure that you are aware of the observable returns subscribed.

How do you catch an observable error?

Catch errors in the observable stream Another option to catch errors is to use the CatchError Operator. The CatchError Operators catches the error in the observable stream as and when the error happens. This allows us to retry the failed observable or use a replacement observable.

What is observable forkJoin?

forkJoin is an operator that takes any number of input observables which can be passed either as an array or a dictionary of input observables. If no input observables are provided (e.g. an empty array is passed), then the resulting stream will complete immediately.

What can I use instead of forkJoin?

concat() which will handle each observable in sequence.


2 Answers

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)) 
like image 186
siva636 Avatar answered Oct 05 '22 23:10

siva636


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

like image 33
Washington Braga Avatar answered Oct 06 '22 00:10

Washington Braga