0

I am trying to build an application in angular but i'm running into a problem when my application is unable to catch an exception thrown inside zone.js (version ^0.8.26).

I'm relatively new to angular but my objective is to have my custom ErrorHandler to catch all exceptions that was not handled inside my components and services. But for some reason the exception was not reaching my ErrorHandler. According to my browser's console the exception is being thrown inside zone.js (line 192), below is the snippet from that file

try {
    if (task.type == macroTask && task.data && !task.data.isPeriodic) {
        task.cancelFn = null;
    }
    try {
        return this._zoneDelegate.invokeTask(this, task, applyThis, applyArgs);
    }
    catch (error) {
        if (this._zoneDelegate.handleError(this, error)) {
            throw error;
        }
    }
}

The HTTP request that was invoking the error is from an observable. Could this mean that the exception is being raised outside my angular app? Below is the flow of my code.

this.xyz.getResult().subscribe(result=> {
  this.callAction(result); //this is expected to throw an HttpErrorResponse, but the exception that was raised from zone.js does not reach my ErrorHandler
});
helloworld2013
  • 3,094
  • 5
  • 19
  • 26

1 Answers1

0

I just encountered exactly the same issue as you and although I have a fix for my application, it may not work for yours.

So based off of your code:

this.xyz.getResult().subscribe(result=> {
  this.callAction(result); //this is expected to throw an HttpErrorResponse, but the exception that was raised from zone.js does not reach my ErrorHandler
});

What I had coming into result originally was a string, say "success". Changing the format from a string to a JSON object such as {status: "success"} allowed the callAction() method to execute as normal. It would be safe to assume that in my case, that the exception was being caused by the format response of the external application. I don't exactly understand why and when Zone.js triggers this error as I cannot recreate it consistently.

It may be worth noting that using FireFox, I only received an Uncaught Exception: [Object object] error. Only when switching to Chrome, did it list the error stack and implicating Zone.js as the suspect.