Home > Mobile >  Calling observable inside an error callback in Angular with RxJs
Calling observable inside an error callback in Angular with RxJs

Time:12-22

I have two observable calls relying on each other, this works fine, but once an error occurs in the response I need to call another observable which rolls the transaction back.

ZThat is my code:

return this.myService.createOrder()
    .pipe(
        concatMap((res: MyResponse) => this.addProduct(res.orderId, PRODUCT_ID))  
    ).subscribe({
          error: (error: any): void => // TODO: Call another observable here passing res.orderId to rollback transaction
    });

As you can see in the TODO my plan is to call another service when an error occurs with res.orderId, but I don't like having nested subscriptions.

Is it possible to do that without creating nested subscriptions???

CodePudding user response:

Don't know if it will solve but could you try with CathError ?

return this.myService.createOrder().pipe(
  concatMap((res: MyResponse) => this.addProduct(res.orderId, PRODUCT_ID)), 
  catchError((res: MyResponse) => {
    // Call here your observable with res
  })
).subscribe(console.log);

CodePudding user response:

As @Emilien has pointed out, catchError is your friend in this case.

catchError expects, as parameter, a function which itself expects an error as input and return an Observable.

So, the code could look like this

// define a variable to hold the orderId in case an error occurs
let orderId: any

return this.myService.createOrder().pipe(
  tap((res: MyResponse) => orderId = res.orderId),
  concatMap((res: MyResponse) => this.addProduct(res.orderId, PRODUCT_ID)), 
  catchError((error: any) => {
    // this.rollBack is the function that creates the Observable that rolls back the transaction - I assume that this Observable will need orderId and mybe the error to be constructed
    // catchError returns such Observable which will be executed if an error ouucrs
    return this.rollBack(orderId, error)
  })
).subscribe(console.log);

As you see, in this case there is only one subscription for the entire chain of Observables.

  • Related