Custom error handler in Zone.js

I worked with Zone.js and I want to configure logging for any uncaught exceptions. I could set up an error handler, for example:

window.onerror = function(e) { 
    //send an error report to the server
}

But this will not work if the exception is thrown inside Promise. The good thing about a zone error handler is that it catches in Promises and throws exceptions for them, but I cannot find a way to actually override or add an error handler after the zone has been created, except overriding a bunch of private fields in the zone.

Is there a real API for the zone that I can use to update the error handler, or do I need to change the polyfill where the root zone is created or overwrite private fields or something like that?

+4
source share
3 answers

You can try something like this:

<html>
<head>
<script src="https://unpkg.com/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 user-defined handler indicated onHandleError, and throw an 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, this does not seem to work if exceptions are thrown directly in the zone. I registered and issue about it.

+2
source

to promise a neutralized promise, from zone.js 0.78, you can use this API.

https://github.com/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);
});
0
source

runGuarded run , ( , setTimeouts Promises ..).

:

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');
});
0
source

Source: https://habr.com/ru/post/1653229/


All Articles