本文介绍了如何获得更好的角度错误堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,当我在生产Angular(v7)应用程序中遇到错误时,我得到了这样的堆栈跟踪.但这实际上是不可能从中获取任何有意义的信息的.如何获得更好的堆栈跟踪,以便缩小此难以捉摸的错误的出处?现在,我正忍受prod中的这个错误,因为它在我的开发机器上从未发生过,并且我不知道如何隔离它,因为stacktrace对我实际上是无用的.我已经习惯了C#之类的语言,在该语言中您会获得非常简洁的stacktrace,从而为您提供错误函数的映射.此堆栈跟踪没有任何意义.

Currently, when I get an error in my production Angular (v7) app, I get a stack trace like this. But this is virtually impossible to get any meaningful information from. How can I get a better stack trace, so that I could narrow down where this elusive error is coming from? Right now, I'm living with this error in prod because it never happens on my development machine, and I have no idea how to isolate it because the stacktrace is virtually useless to me. I am used to languages like C#, where you get a very concise stacktrace that gives you a map down to the function in error. This stack trace has no meaning.

TypeError: Cannot read property 'appendChild' of null
at create_dom_structure (eval at  (:15:1), :1:16231)
at load_map (eval at  (:15:1), :1:101028)
at Object.load (eval at  (:15:1), :1:107834)
at eval (eval at  (:15:1), :1:108251)
at t.invokeTask (https://mywebsite.com/polyfills.d8680adf69e7ebd1de57.js:1:8844)
at Object.onInvokeTask (https://mywebsite.com/main.25a9fda6ea42f4308b79.js:1:467756)
at t.invokeTask (https://mywebsite.com/polyfills.d8680adf69e7ebd1de57.js:1:8765)
at e.runTask (https://mywebsite.com/polyfills.d8680adf69e7ebd1de57.js:1:4026)
at e.invokeTask (https://mywebsite.com/polyfills.d8680adf69e7ebd1de57.js:1:9927)
at invoke (https://mywebsite.com/polyfills.d8680adf69e7ebd1de57.js:1:9818)

我的错误处理程序:

export class AppErrorHandler extends ErrorHandler {

constructor(
    private _http: Http,
    private injector: Injector,
) {
    super();
}

public handleError(error: any): void {
    if (error.status === '401') {
        alert('You are not logged in, please log in and come back!');
    } else {
        const router = this.injector.get(Router);

        const reportOject = {
            status: error.status,
            name: error.name,
            message: error.message,
            httpErrorCode: error.httpErrorCode,
            stack: error.stack,
            url: location.href,
            route: router.url,
        };
        this._http.post(`${Endpoint.APIRoot}Errors/AngularError`, reportOject)
            .toPromise()
            .catch((respError: any) => {
                Utils.formatError(respError, `AppErrorHandler()`);
            });
    }
    super.handleError(error);
}
}

推荐答案

通常Javascript堆栈跟踪 更为有用.不幸的是,在生产应用程序中,您通常会启用缩小功能,这就是为什么您会看到如此混乱的原因.

Generally Javascript stack traces are more useful. Unfortunately, in a production application you generally turn on minification, which is why you get such an unreadable mess.

如果您可以负担得起更大的Javascript捆绑包,则可能会关闭缩小功能,以便在生产中获得更好的堆栈跟踪.

If you can afford a larger Javascript bundle, it may be useful to turn off the minification just to get a better stack trace in production.

如何执行此操作将根据您所使用的Angular CLI版本而有所不同.对于v7 CLI:

How to do this will vary depending on which version of the Angular CLI you're using. For the v7 CLI:

在angular.json文件中,设置以下属性

In the file angular.json, set the following properties

{
  ...
  "projects": {
    "my-project": {
      ...
      "architect": {
        "build": {
          ...
          "configurations": {
            "production": {
              "optimization": false,
              "buildOptimizer": false,
              ...
            }
    ...
}

此问题

这篇关于如何获得更好的角度错误堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 22:51