AngularJS: Override default exception handler

We needed to capture, and log a server error whenever AngularJS framework got any error. Following code snippet helped.

//Override behavior for AngularJS errors
.factory('$exceptionHandler', ['$log', function ($log) {
    return function (exception, cause) {
     //Handle the exception:
     myHandler(exception, cause)
     // Call the default handler. 
     //comment this if you don't want the default AngularJS behavior
     $log.error.apply($log, arguments);

We basically override the existing $exceptionHandler provider with a new one, and handle each and every exception that happens. You can additionally call the existing $log provider to invoke the existing error logging behavior


AngularJS Documentation


2 thoughts on “AngularJS: Override default exception handler

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s