Tim
Tim

Reputation: 225

How can I make another request within catchError?

  this.service.login().pipe(
      catchError((error: HttpErrorResponse, responseObservable: Observable<boolean>) => {
        if (error.status === 400) {
          return this.service.doSomethingElse.pipe(map(() => throwError(error)));
        } else {
          return throwError(error);
        }
      }),
    ).subscribe(

The response from this is always a 200, It won't throw the error when the status code is 400 and return from making the other request. Any help would be great.

Upvotes: 2

Views: 609

Answers (2)

Julius Dzidzevičius
Julius Dzidzevičius

Reputation: 11000

With map() you map values from this.service.doSomethingElse Observable to throwError(error) Observable, so you get stream of streams.

You should flatten it with mergeMap or other flattening operator. Like:

return this.service.doSomethingElse.pipe(mergeMap(() => throwError(error)));

Upvotes: 4

Adrita Sharma
Adrita Sharma

Reputation: 22213

Try this:

this.service.login()
    .subscribe((res: any) => {
     }, error => {
       if (error.status === 400) {
          return this.service.doSomethingElse.pipe(map(() => throwError(error)));
       } else {
           return throwError(error);
       }
 })

Upvotes: 0

Related Questions