Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度错误处理程序包含aot捆绑包上的组件_Angular_Error Handling_Angular2 Aot_Angular Aot - Fatal编程技术网

Angular 角度错误处理程序包含aot捆绑包上的组件

Angular 角度错误处理程序包含aot捆绑包上的组件,angular,error-handling,angular2-aot,angular-aot,Angular,Error Handling,Angular2 Aot,Angular Aot,我有一个errorhandler,看起来像这样: @Injectable() export class GlobalErrorHandler implements ErrorHandler { constructor(private injector: Injector) { } handleError(error) { const errorService = this.injector.get(ErrorService); const location = this.inje

我有一个errorhandler,看起来像这样:

@Injectable() export class GlobalErrorHandler implements ErrorHandler {
constructor(private injector: Injector) { }
handleError(error) {
    const errorService = this.injector.get(ErrorService);
    const location = this.injector.get(LocationStrategy);

const url = location instanceof PathLocationStrategy
? location.path() : '';

StackTrace.fromError(error).then(stackframes => {
    const stackString = stackframes
      .splice(0, 20)
      .map((sf) => {
        return sf.toString();
      }).join('\n');

    const errorObject: IError = {
        errorMessage: error.messagen,
        stackTrace: stackString,
        path: url
    };

    // Display something to user
    errorService.setError(errorObject);

    // TODO: send to server
});

 // IMPORTANT: Rethrow the error otherwise it gets swallowed
 throw error;
  }
}
我是从以下方面得到的:

我的问题是,当我在开发中运行此程序时,它会像预期的那样与包含组件的有意义的stacktrace一起工作:

例如:

恩戈尼尼特()@webpack:///src/app/person/userdetail-page/userdetail-page.component.ts:29:19 __tryOrSetError()@webpack://~/rxjs/Subscriber.js:247:0此项。u tryOrSetError()@webpack://~/rxjs/Subscriber.js:187:0 _next()@webpack://~/rxjs/Subscriber.js:125:0 next()@webpack://~/rxjs/Subscriber.js:89:0 notifyNext()@webpack://~/rxjs/operator/switchMap.js:124:0

但在使用angular cli进行生产时:
ng build--prod--aot

相同错误的输出为:

未定义类型的属性“toString”错误:无法读取属性 下一步在e._的未定义的“toString” () 在e.uuu tryOrSetError () 在e.next

所以这对我来说不是一个有意义的跟踪。如果我能在我的开发环境中得到导致问题的组件,为什么会这样

您如何处理生产站点中的错误?如果我想在代码的每个地方都尝试catch,我可以抛出特定类型的错误,但在没有try-catch块的地方


Stacktrace应该始终显示负责错误的组件,而不仅仅是显示bundle中未定义的tostring

获得此消息的原因是在运行命令
ng build--prod--aot

构建使用绑定和有限的树抖动,而--prod构建也通过UglifyJS运行有限的死代码消除

简言之,所有的错误日志都被缩小了,因此包的大小减小了,这也是我们在生产构建中得到错误消息的原因之一

为了避免这种情况发生,您可以使用此命令,但只能在测试
ng-serve--aot
ng-serve--prod
时使用此命令检查是否有任何错误

AOT编译器在执行过程中检测并报告模板绑定错误 在用户可以看到它们之前执行构建步骤


之所以会出现这种情况,是因为运行命令
ngbuild--prod--aot

构建使用绑定和有限的树抖动,而--prod构建也通过UglifyJS运行有限的死代码消除

简言之,所有的错误日志都被缩小了,因此包的大小减小了,这也是我们在生产构建中得到错误消息的原因之一

为了避免这种情况发生,您可以使用此命令,但只能在测试
ng-serve--aot
ng-serve--prod
时使用此命令检查是否有任何错误

AOT编译器在执行过程中检测并报告模板绑定错误 在用户可以看到它们之前执行构建步骤


好啊为了更好地了解生产构建中出现的问题,我的方法是在angular中创建自己的errorhandler,然后在我的组件中创建关键的东西:尝试{something critical..}catch(ex){setError(ex,PersonDecideComponent.name,'selectionChange');}在我的errorhandler中,我记录了组件名och,以及发生异常的方法。如何考虑该解决方案?在进行aot和prod构建时,无法获得确切的错误。最好使用catch并将错误或引用方法发送到服务器以登录到文件。然后您可以使用该引用进行调试OK。为了更好地了解生产构建中出现的问题,我的方法是在angular中创建自己的errorhandler,然后在我的组件中创建关键的东西:尝试{something critical..}catch(ex){setError(ex,PersonDecideComponent.name,'selectionChange');}在我的errorhandler中,我记录了组件名och,以及发生异常的方法。如何考虑该解决方案?在进行aot和prod构建时,无法获得确切的错误。最好使用catch并将错误或引用方法发送到服务器以登录到文件。然后您可以使用该引用进行调试