Sunday, 5 June 2016

Override the $exceptionHandler to handle the UnHandled error in application globally in Angular Js

In this post we are going to see how to handle the errors globally with custom logic which are unhandled in application, Generally Angular js application handle the unhandled errors and result the errors in Browser console. some times we need to handle the errors globally with some additional functionality. Now we can see how to do that in Angular JS.

They are multiple ways , but we can see the Two ways here. To achieve this we must Override the $exceptionHandler service which handles the Error messages globally.

Way 1:
We can override the service code using the $provide.decorator method, by passing the service name as first parameter, second as function which takes the $delegate as input parameter as mandatory. it must return a function which have two input  parameters exception, cause




output
First Error is because of $delegate execution
Second is because of $log 




















Way 2:
We can Override the service code by create a factory method again in the same name by giving new functionalities like below. It must return a function which must take a two input parameters exception, cause. We have to inject the module to the main module





Output







you may have question in your mind that , why we have to throw exception here instead of calling $delegate, because if we try to inject the $delegate in this factory , it will throw error, i.e it can't be injected or unknown provider, below is the error message sample











Full Source code including both the way of code


























From this post you can learn how to handle the unhandled Error globally and write a custom logic in the application for the Errors




No comments:

Post a Comment