【问题标题】:Angular HttpInterceptor swallowing the errorAngular HttpInterceptor 吞下错误
【发布时间】:2021-08-03 21:30:51
【问题描述】:

我的 Angular 11 应用程序有一个烦人的问题:当我在我的语言环境机器上执行以下代码时,一切都很好,我的 toastrService 会显示正确的错误消息。但是当它被部署到 Azure 时,我总是得到 出了点问题!请再试一次!这是我的错误消息,用于与 HttpErrorResponse 不同的错误(检查组件代码以获取显示的错误消息)。

我有以下拦截方法:

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

    return next.handle(req).pipe(
        catchError(err => {
            // onError
            console.log(err);
            if (err instanceof HttpErrorResponse) {
                console.log(err.status);
                console.log(err.statusText);
                if (err.status === 401) {
                    this.LogoutFromApplication();
                }
            }
            return throwError(err);
        }));
}

以及以下组件逻辑:

this.httpService.post(url, body)
  .subscribe((result) => {
    ...
  },
  (errorResponse) => {
      this.operationFlag = false;
      console.log(JSON.stringify(errorResponse));
      if (errorResponse.error && errorResponse.error.ErrorMessage) {
        this.toastr.showError(errorResponse.error.ErrorMessage);
      } else {
        this.toastr.showError("Something went wrong! Please try again!");
      }
  });

不知何故,错误被拦截器吞没了。拦截器中的 console.log-s 显示错误存在并且它是一个 HttpErrorResponse 但在组件中记录了一个空对象并且 errorResponse 对象似乎未定义。

你能帮我找出问题所在吗?

【问题讨论】:

  • 控制台输出的日志只是errorResponse给出了什么,未定义?
  • 它会抛出一个错误。与此同时,我怀疑根本原因。我们正在使用 adal-angular5 的 Adal5HTTPService,我认为这可能是吞下错误的原因。如果我得到确认,我会更新主题。

标签: angular error-handling interceptor


【解决方案1】:

好的,事实证明确实是 Adal5HttpService 负责。自从这部分在 rxjs 中更改后的几年中,它似乎错误地抛出了错误。所以上面问题中的代码运行正常,这个问题和调查的信息是

不要将搜索范围缩小到问题最明显的地方,尝试查看全貌。

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 2011-10-09
    • 2021-01-28
    • 1970-01-01
    • 2016-01-27
    相关资源
    最近更新 更多