我有一个看起来像这样设置ErrorHandler:角的ErrorHandler包括AOT捆绑组件
@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
但在生产中使用的角度CLI时:ng build --prod --aot
的输出是相同的错误是:
property 'toString' of undefined TypeError: Cannot read property 'toString' of undefined at e._next (http://xxx.azurewebsites.net/main.b21b245638698421733f.bundle.js:1:5701) at e.__tryOrSetError (http://xxx.azurewebsites.net/vendor.1cd9b81fc017fd7eac16.bundle.js:835:16880) at e.next
所以这不是对我来说有意义的堆栈跟踪。如果我能在某些为什么会在我的开发环境中得到导致问题的组件?
你如何处理生产现场的错误?如果我想尝试在我的代码中的每个地方捕捉,我可以抛出特定类型的错误,但在没有尝试catch块的地方?
Stacktrace应该始终显示负责该错误的组件,而不是仅显示捆绑中未定义的tostring!
确定。我的方法得到一个更好的输入在我的生产构建出了什么问题是让我自己的错误处理程序角度,然后在我的组件关键的东西正在进行: 尝试{ 某些关键.. } catch(ex ){ setError(ex,PersonDecideComponent.name,'selectionChange'); } 在我的错误处理程序即时消息组件名称和发生异常的方法。该怎么考虑这个解决方案? – NetProvoke
当你执行aot和prod时,你不能得到确切的错误,最好的办法是使用catch并发送错误或方法供你参考服务器登录到一个文件。然后你可以使用该引用进行调试 –