How to delete file not found error in angular js

I am trying to handle an exception in angular js. I can handle in one script, but scripted it can't handle why?

Actually, when anything is undefined, it catches my $ exceptionHandler. But when the file is not found it is not caught by $ exceptionHandler why

Plunker example http://plnkr.co/edit/mm4Uix8XXVrkhexW15vR?p=preview

case 1

when i hit this

$http.get("default.json").then(successcallback, errorcallback);

      

on success alert(data.hhh.getname()); it is undefined which is catch by

$ exceptionHandler and store it in an array. Here is the outputenter image description here

case 2 But when I change the name of the default.son file to defaul.json it is not a trap $ exceptionHandler, why? and it shows 404 error on console.can we catch this error and print to console? we can catch this error.?

I actually don't want to get this error enter image description here

how to deal with this error?

+3


source to share


1 answer


See the documentation for $ exceptionHandler . Pay attention to the part:

Please note that this code executed on event-listeners (even those registered using jqLite's on / bind methods) does not delegate exceptions to $ exceptionHandler (unless executed during the digest).

This means it is $exceptionHandler

catching an uncaught exception from the digest loop . However, if you actually click on the line that has 404 selected, you should notice that it was selected by XHR outside of Angular $ apply , so $exceptionHandler

it is out of scope outside of it to catch it. Yours, on the other hand, data.hhh.getname()

is in the $ q callback that is wrapped in $ apply , which is why it got caught $exeptionHandler

.

EDIT Actually the digest loop related article explains it better.

Keep in mind that you should always use the version of $ apply () that takes a function argument. This is because when you pass a function to $ apply (), the function call is completed inside the try ... catch block and any exceptions that are thrown will be passed to the $ exceptionHandler service.



This is more than just a digest loop, it must be inside the $ apply scope . Otherwise Angular won't be able to put the block try ... catch

anywhere to catch exceptions and pass it to $exceptionHandler

.

EDIT On closer inspection, the 404 you see on the console is not even an exception. He signed up for console.error

XHR , which is a native API. So, what you see on the console should stay and cannot be caught. Note, however, that you can always check for errors $http

in your callback so that you can insert $exceptionHandler

(which I see you set up yourself) and manually register with it. eg.

responseError: function(error){
    $exceptionHandler(new Error(
            error.config.method + ' ' +
            error.config.url + ' ' +
            error.status +' ' +
            '(' + error.statusText + ')'
    )); 
    return $q.reject(error);
}

      

See Plunker

+1


source







All Articles