【发布时间】:2018-02-25 05:54:47
【问题描述】:
我有一个看起来像这样的错误处理程序:
@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
我的问题是,当我在开发中运行它时,它会按预期工作,其中包含组件的有意义的堆栈跟踪:
例如:
ngOnInit()@webpack:///src/app/person/userdetail-page/userdetail-page.component.ts:29:19 __tryOrSetError()@webpack:///~/rxjs/Subscriber.js:247:0 this.__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._next 处未定义的“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 应该始终显示导致错误的组件,而不仅仅是显示捆绑包中未定义的字符串!
【问题讨论】:
标签: angular error-handling angular2-aot angular-aot