Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Override The AngularJS Exception Handler

AngularJS is one of the hottest things around right now. Maybe you’re finding yourself getting pretty deep in its greatness and you’re ready to customize how exceptions are handled by default.

Lucky for us there is a way to override the default functionality and do whatever we want when an error happens.

Now most of what I’m going to show you is straight from the AngularJS documentation, but if you’re anything like me, you had a tough time with what the documentation was talking about.

To keep things simple, we’re just going to customize the message that shows up in the browser console.

Let’s start by creating a custom AngularJS module called exceptionOverride which will contain our new $exceptionHandler factory:

angular.module('exceptionOverride', []).factory('$exceptionHandler', function() {
    return function(exception, cause) {
        exception.message += ' (caused by "' + cause + '")';
        throw exception;

Now let’s check out the modification in action. In your project’s main AngularJS module, do something like the following:

var example = angular.module("example", ['exceptionOverride']);
example.controller("ExampleController", function($scope) {
    var x = n + 1;

You can probably tell that n is undefined in this example. Based on our custom exception message, the browser logs show an error that looks like the following:

ReferenceError: Can't find variable: n (caused by "undefined")

Pretty awesome right? If we didn’t override the exception handler we would have ended up with a messy error that looks like this:

[Error] Error: Can't find variable: n
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:35:41
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:35:166
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:7:396
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:53:397
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:47:257
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:47:274
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:113:32
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:113:315
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:35:41
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:18:184
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:18:388
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:17:416
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:146:94
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:7:294
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js:31:208
    (anonymous function) (angular.min.js, line 92)
    (anonymous function) (angular.min.js, line 68)
    $apply (angular.min.js, line 113)
    (anonymous function) (angular.min.js, line 18)
    d (angular.min.js, line 35)
    c (angular.min.js, line 18)
    dc (angular.min.js, line 18)
    Wc (angular.min.js, line 17)
    (anonymous function) (angular.min.js, line 216)
    a (angular.min.js, line 146)
    (anonymous function) (angular.min.js, line 31)
    r (angular.min.js, line 7)
    c (angular.min.js, line 31)

Now how would we have been able to determine n was undefined from that?

As we saw it wasn’t difficult to override the exception handler, and it opens your AngularJS world to more possibilities. Why not send an HTTP request of the error to some remote web server for tracking? Plenty of things that could be done.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.


Follow Us

The Polyglot Developer


Subscribe to the newsletter for monthly tips and tricks on subjects such as mobile, web, and game development.

The Polyglot Developer

Support This Site