Angular 5 HTTPCLIENT错误响应不可捕获



我们正在使用Angular 5和一个Spring 2 Oauth后端。现在,当我发送一个旧的令牌时,它当然已经过期了。它返回状态代码:401,以及无效令牌等错误响应。现在,我在日志中或遇到错误时看不到它。我想遇到错误,以便首先可以记录它,然后在刷新令牌或将其发送到登录页面上。

现在,如果我订阅请求:

.subscribe(res => {
    //just random stuff.
    }, err => {
    console.log("error", err);
});

我只是在日志中看到此响应,如此图像中的未知错误

后端可能是失败吗?因为我还会在日志中看到类似"''''''访问控制 - 允许 - 孔"标头的东西 - eRror,尽管这是因为无效的令牌。

尽管我可以在Google Chrome Dev工具中看到此响应代码和401状态代码。

所以我试图自己找到解决方案。我已经有一个拦截器,并使用了一些解决方案

return next.handle(authReq)
  .catch(error => {
    console.log("im in here");
    console.log(error);
    return Observable.throw(error);
  });

HTTP服务只是丢弃了一个错误,即使没有记录错误或" IM中的im in there"。

我还尝试了下一步的.do。

.do((event: HttpEvent<any>) => {
    if (event instanceof HttpResponse) {
      // do stuff with response if you want
    }
  }, (err: any) => {
  console.log(err);
    if (err instanceof HttpErrorResponse) {
      if (err.status === 401) {
      }
    }
  });

我尝试了http之后的管道。

http.get(...).pipe(
  retry(3), // retry a failed request up to 3 times
  catchError(this.handleError) // then handle the error
);

    import 'rxjs/add/operator/catch';
    Somefunc(){
    this.httpClient
          .get("data-url")
          .subscribe(
            data => console.log('success', data),
            error => console.log('oops', error)
          );
    }

    OR
this.httpClient
      .get("data-url")
      .catch((err: HttpErrorResponse) => {
        // simple logging, but you can do a lot more, see below
        console.error('An error occurred:', err.error);
      });

应该工作。

最新更新