Angular HttpInterceptor最终从不会调用错误

问题描述 投票:0回答:1

我正在打多个电话,在网络选项卡中我可以看到除一个以外的所有电话。但是由于某种原因,如果其中一个拥有404,则其他呼叫则永远不会finalize。这是预期的行为吗?我正在运行角度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

angular rxjs6 angular-http-interceptors
1个回答
0
投票

拦截器作为中间件工作,所以如果一个拦截器掉了,错误就会上升,直到被捕获为止。

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

一些拦截器中的简单捕获错误以阻止其传播。

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

          this.progressService.decrease();
        }),
      );
© www.soinside.com 2019 - 2024. All rights reserved.