我可以不使用catchError

时间:2018-12-27 09:49:49

标签: angular typescript angular6 angular-http-interceptors

在角度6中,如果我得到响应,则我使用拦截器调用http服务 {“状态”:403,“消息”:“获取数据时出错,请重试”}

我想再次调用该服务。 可以使用catchError,有没有其他方法可以做到没有catchError,即我不能使用错误代码来做到这一点。我想在响应正文中使用我的状态代码

下面的代码在调用success200方法时再次调用请求不起作用。它仅在catchError中起作用

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
    const authService = this.injector.get(UserTokenService);

    return next.handle(this.addToken(req, authService.getToken())).pipe(
        tap(
            event => 
            {   
              if (event instanceof HttpResponse) {
                return this.success200(req, next,event.body);
              }
            },
            error => status = 'failed'
          ),

          catchError(error => {
     //This will work
    return next.handle(this.addToken(req,  authService.getToken()));
        })

        );
}






//All Success request comes here
success200(req: HttpRequest<any>, next: HttpHandler,response){
    const authService = this.injector.get(UserTokenService);
    const routerService = this.injector.get(Router);

    if(response.status=="403"){
      //this call not working

      return next.handle(this.addToken(req,authService.getToken()));
    }

    else{

        return;
    }

}

addToken(req: HttpRequest<any>, token: string): HttpRequest<any> {

    //req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
    if(token != null) {
    //set token header with all request
        return req.clone({ setHeaders: { Authorization: token }});
    }
    else {
        return req;
    }

}

1 个答案:

答案 0 :(得分:0)

retry运算符很了解。例如:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
  const authService = this.injector.get(UserTokenService);

  return next.handle(this.addToken(req, authService.getToken())).pipe(
    flatMap(
        event => 
        {   
          if (event instanceof HttpResponse) {
            return this.success200(req, next,event.body);
          }
          else {
            return throwError('Failed');
          }
        }
      ),
      retry(1)  // if you want to invoke 'next.handle' 1 additional time
  );
}

我尚未测试此代码,但这是一个基本概念,您可以在下面提供的链接中查看示例。

我已经创建了this个堆栈闪电示例,供您查看retry的运行情况。

更新:

我做了一些其他研究,发现只要 http 调用发生 403 错误,您就会自动进入catch块。我创建了另一个stackblitz示例,您可以在其中查看 200 403 响应的 http 请求中发生的情况。至于您的示例,您也可以尝试这样的操作:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
  const authService = this.injector.get(UserTokenService);

  return next.handle(this.addToken(req, authService.getToken())).pipe(
    flatMap(
        event => 
        {
           if (event instanceof HttpResponse) {
              return this.success200(req, next,event.body);
           }
        }
      ),
      catchError(e => {
        console.log('Failed attempt to login');
        return throwError('Failed');  // retry logic should automatically kick in whenever _throwError_ occurs
      }),
      retry(1)  // if you want 1 additional time
  );
}