代码之家  ›  专栏  ›  技术社区  ›  dotnet-provoke

aot束上的角度错误处理程序包含组件

  •  1
  • dotnet-provoke  · 技术社区  · 7 年前

    @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;
      }
    }
    

    Global error handling angular 2

    例如:

    __tryOrSetError()@webpack:///~/rxjs/Subscriber。js:247:0这个__tryOrSetError()@webpack:///~/rxjs/Subscriber。js:187:0 notifyNext()@webpack:///~/rxjs/operator/switchMap。js:124:0

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

    相同错误的输出为:

    未定义TypeError的属性“toString”:无法读取属性 http://xxx.azurewebsites.net/main.b21b245638698421733f.bundle.js:1:5701 ) 在e.__tryOrSetError http://xxx.azurewebsites.net/vendor.1cd9b81fc017fd7eac16.bundle.js:835:16880 ) 在e.next

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

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   Rahul Singh    7 年前

    之所以会出现这种情况,是因为在运行命令时 ng build --prod --aot .

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

    在用户可以看到它们之前的构建步骤。