捕获到的Angular 2错误的堆栈跟踪不可用。更好的方法?

时间:2019-05-30 22:03:41

标签: angular

我使用this method捕获标准JavaScript全局错误或捕获无法捕获的JavaScript错误。目的是打开用户可以发送给我们的错误报告电子邮件,以便我们修复该错误。

但是,以这种方式捕获的错误所产生的堆栈跟踪是完全不可用的。当我禁用此错误捕获器时,会得到类似以下的东西:

zone.js:199 Uncaught TypeError: Cannot create property 'fullString' on string ''
    at Function.push.../shared/directives/fullName.ts.FullNameObj.updateFullStringStatic (fullName.ts:31)
    at FullName.push.../shared/directives/fullName.ts.FullName.updateFullNameString (fullName.ts:146)
    at FullName.push.../shared/directives/fullName.ts.FullName.ngOnInit (fullName.ts:108)
    at checkAndUpdateDirectiveInline (core.js:22098)
    at checkAndUpdateNodeInline (core.js:23362)
    at checkAndUpdateNode (core.js:23324)
    at debugCheckAndUpdateNode (core.js:23958)
    at debugCheckDirectivesFn (core.js:23918)
    at Object.eval [as updateDirectives] (ReportField.html:56)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:23910)

在这种情况下,我遇到了确切的问题:指令/fullName.ts是我感兴趣的文件,而updateFullString()是相关的方法。

但是当我启用全局Angular Error Handler时,它捕获了相同的错误,其堆栈跟踪为:

Error    
at http://localhost:8087/report/vendor.js:54045:87    
at ZoneDelegate.push.../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:8087/report/polyfills.js:3284:26)    
at Zone.push.../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:8087/report/polyfills.js:3043:43)    
at NgZone.push.../node_modules/@angular/core/fesm5/core.js.NgZone.runOutsideAngular (http://localhost:8087/report/vendor.js:53177:28)    
at ApplicationRef.push.../node_modules/@angular/core/fesm5/core.js.ApplicationRef.tick (http://localhost:8087/report/vendor.js:54045:24)    
at http://localhost:8087/report/vendor.js:53929:105    
at ZoneDelegate.push.../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:8087/report/polyfills.js:3284:26)    
at Object.onInvoke (http://localhost:8087/report/vendor.js:53218:33)    at ZoneDelegate.push.../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:8087/report/polyfills.js:3283:52)    
at Zone.push.../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:8087/report/polyfills.js:3043:43)

我的全局错误处理程序是:

@Injectable()
export class GlobalErrorHandler implements ErrorHandler
{
    constructor(private injector: Injector, private globals: CRGlobals)
    {

    }

    handleError(error)
    {
            // UNCOMMENT this (and comment out the rest) to get a better error report in the console log!
    //  throw error;

        if (this.globals.hasVisitedDeveloperPage)
        {
            console.log("Throwing globally caught error, since user has visited developer page");
            throw error;
        }
        else
        {
        //  const loggingService = this.injector.get(LoggingService);
            const message = error.message ? error.message : error.toString();
            // log on the server
        //  loggingService.log({ message });

                // Certain errors we don't want to report
            if (message.indexOf("ExpressionChangedAfterItHasBeenCheckedError") == -1)
            {
                let obj = {};
                (<any>Error).captureStackTrace(obj, this.handleError);  // This works in QWebEngine and is supposed to work in Chrome, too.


                ReportNetwork.reportErrorToAIM( this.globals, "An Angular 2 global error was caught: " + error + "\n\n" + (<any>obj).stack, "(globally uncaught code)");
            }
            else
                Utils.logInColor("orange", "An Angular 2 error was caught but ignored: " + message, error);
        }

    }
}

如何捕获此错误并在未捕获到错误的情况下向用户报告在JavaScript控制台中看到的堆栈跟踪?

0 个答案:

没有答案