How to track Angular.js exceptions automatically

Callum GavinProvider Updates1 Comment

One of the areas where Angular.js has made great gains is the usefulness of the exceptions it throws, as the messages often indicate exactly why your code broke. The problem with having a huge client-side web app running on a multitude of browsers across the world is that these exceptions are lost, and with it the chance to fix up the bug and win back a user.

AngularJS-logo

It is especially important to receive these exceptions when dealing with cross-browser and device issues, as while your app may perform rock-solid on your dev machine, the browsers on your user’s machines are guaranteed to be different creating an infeasibly large testing surface.

The solution is an automatic exception tracking service, and Raygun makes this easy by receiving every exception your Angular web app throws, without you doing a thing. It’s really quick to set up, too – follow the steps below to hook Raygun into your app.

Installation

First, download the tiny Raygun4JS provider script and add it to your project. There’s three delivery channels:

Via Bower

From NuGet – in Visual Studio, open up the Package Manager Console and type

Manual download – grab the dev version here, or the minified production version here.

Configuration

Next, reference the script – if you’re using static HTML add something like <script src="js/raygun.js" type="text/javascript"></script> or add it to your module loader/minifier.

Finally, call the following code before your main Angular logic to set up Raygun4JS:

You get an API key for each app you create in Raygun, which is available in your Raygun dashboard – there’s a free 30-day trial available for you to test it out.

Catching exceptions in Angular

There are at least two options for injecting an unhandled exception function into Angular.js modules, with a decorator or a factory. Both of these provide your specified implementation of $exceptionHandler, which we will use to send to Raygun via the Raygun4JS provider we referenced above.

Using a decorator

The Decorator pattern is great for injecting behaviour onto arbitrary services ensuring separation of concerns among other desirable properties, and is the preferred way for logging and exception handling as it doesn’t override the original behaviour. In Angular.js this is available on the $provide service, which we will use to implement our own $exceptionHandler function:

The $delegate is the instance of the exception handler, which we call to get the original behavior of outputting to the console.

You can also make as many other services available as required:

Depending on what sort of error gets thrown within Angular logic, the cause parameter can be populated. If one occurs within a factory or service you may get the scope available on that parameter, which you can transmit to Raygun as custom data, along with anything else you need, by replacing the Raygun.send call above:

Using a factory

A quick way for getting Raygun into your app’s exception handler is to override it using a factory, although this removes the original console logging and you’ll need to store the original value then call it again, if you want this functionality.

Manually sending errors

Raygun4JS also gives you the ability to manually track errors easily at any time, like this:

There’s also a bunch of other useful tools available on the provider, including unique user tracking, version tracking, tags and more – check out the documentation here for all the details.

Raygun even tracks jQuery Ajax errors in your Angular app without any extra effort needed from you, so you get full coverage out-of-the-box.

Ready to get Raygun?

As mentioned above there’s a free 30-day, no-credit-card trial available here, so grab yours and check out how your web app is really performing for your users. If you have any questions about this post leave them in the comments below, otherwise happy error blasting!

Next level software intelligence across your entire stack.

One Comment on “How to track Angular.js exceptions automatically”

Leave a Reply

Your email address will not be published. Required fields are marked *