0

In my one of the Angular application, I have two objects of types Observable<Object1[]> and Observable<Object2[]>, which call different apis in the resolver as below:

resolve(): Observable<[Array<Object1>, Array<Object2>]> {
    const object1 = this.bookingService.executeService1(); // returns Observable<Object1[]>

    const object2 = this.bookingService.executeService2(); // returns Observable<Object2[]>

    return Observable.forkJoin(object1, object2); // Need to do error handing here
}

It works fine unless and until the service returns an error. On error, it shows in the console as Uncaught. I have checked the service api through apps like postman, and it does works fine through it.

I tried to catch it as:

return Observable.forkJoin(object1, object2)
  .catch(error => {
    console.log('error', error);
  });

But it doesn't help.

Let me know if there is any way to catch the error in the resolver through Observable.forkJoin();

Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59
Shashank
  • 2,010
  • 2
  • 18
  • 38

3 Answers3

2

You could find the answer for your concern in the forkJoin's learnrxjs.io page.

Be aware that if any of the inner observables supplied to forkJoin error you will lose the value of any other observables that would or have already completed if you do not catch the error correctly on the inner observable. If you are only concerned with all inner observables completing successfully you can catch the error on the outside.

const example = forkJoin(
  //emit 'Hello' immediately
  of('Hello'),
  //emit 'World' after 1 second
  of('World').pipe(delay(1000)),
  // throw error
  _throw('This will error')
).pipe(catchError(error => of(error)));
ibenjelloun
  • 7,425
  • 2
  • 29
  • 53
0

You can use it as mentioned in RxJS Angular2 handling 404 in Observable.forkjoin .

Hope it will be helpful to you !

0

You can catch the error in this way :
let first = Observable.of({value : 1}); let second = Observable.throw({value : 2}); Observable.forkJoin(first, second) .subscribe((res: Array<any>) => console.log(res), (error) => { console.log(error); //{value: 2} will get displayed here });

You can also catch the error in each observable independently and return another observable in such cases.
return Observable.forkJoin( this.http.get('/url') .map((res:Response) => res.json()) .catch(res:Response => Observable.of({}), this.http.get('/url2') .map((res:Response) => res.json()) .catch(res:Response => Observable.of({}), );

Pranav Pavan
  • 734
  • 6
  • 4
  • Although it logs the error, but it is still Uncaught and shows Uncaught error in the console, which shouldn't happen. – Shashank Apr 12 '18 at 09:17