小编Ale*_*leG的帖子

Angular 7 - 如何在某些响应状态代码上重试 http 请求?

我正在尝试从 Angular 拦截器捕获 http 请求的错误,并在重试 503 和 504 响应n时间时将401 处理为注销。

这是我的 http 拦截器:

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

    return next.handle(req).pipe(
      catchError(error => {
        if (error.status === 401) {
          this.authenticationService.logout();
          this.router.navigate(['login']);
        }

        return throwError(error);
      }),
      retryWhen(errors => errors
        .pipe(
          concatMap((error, count) => {
            if (count < 2 && (error.status == 503 || error.status == 504)) {
              return of(error.status);
            }

            return throwError(error);
          }),
          delay(500)
        )
      )
    );
  }
Run Code Online (Sandbox Code Playgroud)

我 100% 确定这段代码在我编写时有效,因为我对其进行了多次测试,但现在它给了我:

UnsubscriptionErrorImpl 
{message: "1 errors occurred during unsubscription:?1) TypeError: …
Run Code Online (Sandbox Code Playgroud)

http rxjs typescript angular retrywhen

6
推荐指数
1
解决办法
3886
查看次数

标签 统计

angular ×1

http ×1

retrywhen ×1

rxjs ×1

typescript ×1