【问题标题】:Angular get all console errorsAngular获取所有控制台错误
【发布时间】:2020-07-22 03:00:03
【问题描述】:

我正在尝试捕获我的 Angular 应用程序的所有控制台错误,不仅是 http 响应错误,还有那些在应用程序中生成的错误。我的意思是,所有控制台错误,像这样

或者像这样

或者像这样

我需要的是,当我捕捉到一些此类错误(应用程序错误和 http 错误)时,我将其放入数据库的另一个服务中以存储问题并进行一些统计。

我研究过并遇到了以下解决方案:

一、角度解法--->https://angular.io/api/core/ErrorHandler

这也是答案 ---> How to get all console error messages in Angular application

这个---->Angular error handling and logging - Call GlobalErrorHandler's handleError

还有这个---->https://www.concretepage.com/angular/angular-custom-error-handler

但似乎都不起作用(即使是 http 响应也不行)

我正在尝试集中执行此操作,我的意思是,一个可以捕获所有错误的服务,无论发生错误的模块或组件如何,然后将错误发送到我的其他服务并将错误存储在我的数据库中。 .. 这可能吗?怎么可能?

非常感谢!

【问题讨论】:

    标签: angular typescript error-handling console-application angular-errorhandler


    【解决方案1】:

    console.error 方法的猴子补丁肯定会帮助您捕获所有控制台错误。

    console.error = (originLogFn => function () {
      // do smth with the error
    
      originLogFn.apply(this, arguments);
    })(console.error);
    

    还要注意,在 Angular 初始化之前可能会发生错误,并且它不会被任何带有 console.error 的 Angular 内置服务处理。在这种情况下,您可以简单地使用:

    window.onerror = (err) => {
      // do smth with the error
    }
    

    【讨论】:

    • 非常感谢...我应该把这段代码放在哪个文件中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    相关资源
    最近更新 更多