Ricardo Saracino
Ricardo Saracino

Reputation: 1420

Angular HttpInterceptor Finally Never Called on Error

I am making multiple rest calls and i can can see all but the one failing in the network tab. but for some reason if one of them has a 404 the other calls never finalize. Is this expected behavior? Im running angular 8.2

 public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    console.log(`${request.method} '${request.urlWithParams}' ProgressInterceptor ++.`);

    this.progressService.increase();

    return next.handle(request)
      .pipe(
        finalize(() => {
         console.log(`${request.method} '${request.urlWithParams}' ProgressInterceptor --.`);

          this.progressService.decrease();
        }),
      );
  }

enter image description here

Upvotes: 1

Views: 1096

Answers (1)

Michał Dziwota
Michał Dziwota

Reputation: 948

interceptors work as middlewares, so if one falls, error goes up until it will be caught.

interceptor1.handle(request)
    .pipe(
        switchMap(() => interceptor2.handle(request))
            .pipe(
                switchMap(() => interceptor3.handle(request))
                    .pipe(
                    ...
                    )
            )
    )
.subscribe()

simple catch error in some interceptor to stop it propagate.

return next.handle(request)
      .pipe(
        catchError(error => of('error caught')),
        finalize(() => {
         console.log(`${request.method} '${request.urlWithParams}' ProgressInterceptor --.`);

          this.progressService.decrease();
        }),
      );

Upvotes: 2

Related Questions