【问题标题】:Angular 6 HttpInterceptor prevent output of errorsAngular 6 HttpInterceptor 防止输出错误
【发布时间】:2018-09-19 21:02:33
【问题描述】:

我想要一个 HTTPInterceptor 来处理所有错误。我的代码如下所示:

return next.handle(req)
  .pipe(
    catchError((response: any) => {
      if (response instanceof HttpErrorResponse) {
        if (response.status === 401) {
          console.log('error catched';

          return empty();
        }
      }

      return throwError(response);
    })
  );

但在运行代码时会产生 401 错误

return this.http
  .post(`${environment.baseAPIUrl}/identity/login`, { 'user': user, 'pwd': pwd})
  .pipe(
    tap(res => this.setToken(res)),
    shareReplay()
  );

输出是这样的:

POST http://xyz 401 (Unauthorized)
error catched

所以我可以处理错误,但我无法阻止错误输出。我做错了什么?

最好的

英格玛

【问题讨论】:

    标签: angular angular-http-interceptors catch-exception


    【解决方案1】:

    您是否尝试过放入调试器语句,然后单步执行代码以查看它是否仍在下一行?

    return throwError(response);
    

    我同意你的观点,如果它没有达到那条线,它似乎不应该冒泡错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2013-11-13
      • 2011-06-16
      • 1970-01-01
      相关资源
      最近更新 更多