admin管理员组文章数量:1303451
I've been working with Zone.js and I want to set up logging for any uncaught exceptions. I could set up an error handler, like so:
window.onerror = function(e) {
//send an error report to the server
}
But this won't work if an exception is thrown inside a Promise. The nice thing about the Zone error handler is that it hooks into Promises and does fire for exceptions in them, but I can't find a way to actually override or add an error handler after the Zone has been created aside from overriding a bunch of private fields on the Zone.
Is there an actual API for a Zone that I can use to update the error handler, or do I have to change the polyfill where the root Zone is constructed or overwrite private fields or something similar?
I've been working with Zone.js and I want to set up logging for any uncaught exceptions. I could set up an error handler, like so:
window.onerror = function(e) {
//send an error report to the server
}
But this won't work if an exception is thrown inside a Promise. The nice thing about the Zone error handler is that it hooks into Promises and does fire for exceptions in them, but I can't find a way to actually override or add an error handler after the Zone has been created aside from overriding a bunch of private fields on the Zone.
Is there an actual API for a Zone that I can use to update the error handler, or do I have to change the polyfill where the root Zone is constructed or overwrite private fields or something similar?
Share Improve this question asked Aug 31, 2016 at 23:39 Owen K.Owen K. 811 silver badge3 bronze badges 2-
You can save reference to
Promise
constructor before executing Zone.js script. – user6586783 Commented Aug 31, 2016 at 23:46 - @SoftwareEngineer171 I don't see how that helps me. – Owen K. Commented Sep 1, 2016 at 16:37
3 Answers
Reset to default 3for promise uncaught promise error, from zone.js 0.78, you can use this API.
https://github./angular/zone.js/pull/627
window.addEventListener("unhandledrejection", function (event) {
console.warn("WARNING: Unhandled promise rejection. Shame on you! Reason: "
+ event.reason);
});
window.addEventListener("rejectionhandled", function (event) {
console.log("Promise rejected! Reason: " + reason);
});
You could try something like this:
<html>
<head>
<script src="https://unpkg./zone.js?main=browser"></script>
<script>
Zone.current.fork({
onHandleError: function(parentZoneDelegate, currentZone, targetZone, error) {
console.log("Error handled by zone: " + error);
}
}).run(function () {
setTimeout(function () {
console.log("In zone setTimeout")
throw new Error("Throw in zone setTimeout");
}, 0);
console.log("Directly in zone");
});
</script>
</head>
<body>
</body>
</html>
Which would catch the exception in the custom handler specified by onHandleError
and yield output like this:
Directly in zone (test1.html:14)
In zone setTimeout (test1.html:11 )
Error handled by zone: Error: Throw in zone setTimeout (test1.html:7)
However it seems it does not work if exceptions are thrown directly in the zone. I have filed and issue about this.
You have to use runGuarded
method instead of run
on Zone if you want to handle all types of unhandled exceptions (in simple zones, in setTimeouts or in Promises, etc.)
Here is a sample code to do that:
Zone.current.fork({
name: 'MyGlobalErrorLoggerZone',
onHandleError: function(parentZoneDelegate, currentZone, targetZone, error){
console.log(error);
// send it to server here.
}
})
.runGuarded(function(){
// your application code here...
// handles errors inside promises
new Promise(function(resolve, reject){
reject('Rejecting: Some Error');
});
console.log('Running in Zone...', Zone.current.name);
// handles errors inside timeouts/intervals
setTimeout(function(){
console.log('after 1 sec', Zone.current.name);
throw new Error('This is an error. Make your self log me please.');
}, 1000);
// Handles errors throw directly in zone too.
throw new Error('oops, one more error');
});
本文标签: javascriptCustom error handler in ZonejsStack Overflow
版权声明:本文标题:javascript - Custom error handler in Zone.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741749168a2395719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论