首页 > 解决方案 > 为什么在服务器关闭时无法在 Angular 中捕获 HTTP 错误?

问题描述

我的 Angular 9 中有一个 REST 服务来处理所有 HTTP 请求。当我关闭 REST 服务器时,出现错误console.log()但我无法在服务中捕获它们:

    return this.http.post<any[]>(this.path, data, header).pipe(
        catchError((err: HttpErrorResponse) => {
            console.log("---", err);

            return of(null);
        })
    );

控制台输出始终是:

    zone-evergreen.js:2845 HEAD https://SERVER/ net::ERR_CERT_COMMON_NAME_INVALID

为什么会发生这种情况,为什么会catchError被忽视?

标签: angularrxjsangular-httpclientangular-http

解决方案


对于与 http 调用相关的错误,您将在 catchError 中收到错误。在这种情况下,您的呼叫甚至没有到达任何服务器。为了捕获这些类型的错误,您可以通过实现 ErrorHandler 接口来设置全局错误处理程序。请参阅下面的链接以执行此操作:-

https://medium.com/@aakashgarg19/advanced-exception-handling-with-power-of-dependency-injection-in-angular-93cd7422f051


推荐阅读