Skip to main content
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes and Ezra Parker and Dan Wilson and John Mason and Jason Dean and Luis Majano and Mark Mandel and Brian Kotek and Wil Genovese and Rob Brooks-Bilson and Andy Matthews and Simeon Bateman and Ray Camden and Chris Rockett and Joe Bernard and Dan Skaggs and Byron Raines and Barney Boisvert and Simon Free and Steve 'Cutter' Blades and Seth Bienek and Katie Bienek and Jeff Coughlin
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes Ezra Parker Dan Wilson John Mason Jason Dean Luis Majano Mark Mandel Brian Kotek Wil Genovese Rob Brooks-Bilson Andy Matthews Simeon Bateman Ray Camden Chris Rockett Joe Bernard Dan Skaggs Byron Raines Barney Boisvert Simon Free Steve 'Cutter' Blades Seth Bienek Katie Bienek Jeff Coughlin

Quick Note: Favor $exceptionHandler() Over $log.error() In AngularJS

By
Published in

This is just a really minor note. I was looking through some AngularJS code the other day and came across someone using a try-catch block that passed the error off to the $log.error() method. While this works - it logs the error to the console - it doesn't add as much value as the $exceptionHandler() service. There are times when logging to the console makes sense; but, as a general rule, I would try to favor the $exceptionHandler() service over the $log.error() method in the vast majority of cases.

<!doctype html>
<html ng-app="Demo">
<head>
	<meta charset="utf-8" />

	<title>
		Quick Note: Favor $exceptionHandler() Over $log.error() In AngularJS
	</title>
</head>
<body ng-controller="AppController">

	<h1>
		Quick Note: Favor $exceptionHandler() Over $log.error() In AngularJS
	</h1>

	<p>
		<em>View the console</em>.
	</p>

	<!-- Load scripts. -->
	<script type="text/javascript" src="../../vendor/angularjs/angular-1.4.2.min.js"></script>
	<script type="text/javascript">

		// I control the root of the application.
		angular.module( "Demo", [] ).controller(
			"AppController",
			function provideAppController( $scope, $log, $exceptionHandler ) {

				// ** Avoid. **
				// --
				// Using the $log.error() method will safely log the error to the console
				// if it is available. If the console is not available, the error will
				// just be swallowed quietly.
				try {

					throw( new Error( "Something went boom."))

				} catch ( error ) {

					$log.error( error );

				}


				// ** Recommended. **
				// --
				// By default, the $exceptionHandler() service does the same thing as
				// $log. The default implementation actually just turns around and calls
				// $log.error() internally. However, the entire framework defers to the
				// $exceptionHandler() for all try / catch blocks which makes it a
				// perfect place to track errors. By favoring $exceptionHandler() over
				// the $log.error() method, you keep in alignment with the rest of the
				// code and make your errors more readily available.
				try {

					throw( new Error( "Something went boom."))

				} catch ( error ) {

					$exceptionHandler( error );

				}

			}
		);

	</script>

</body>
</html>

Out of the box, the default implementation of the $exceptionHandler() service just turns around and calls the $log.error() method. So, you get console-logging for free. The real benefit of the $exceptionHandler() service, however, is two fold: For starters, the rest of the framework uses $exceptionHandler() to manage errors. So, by using the $exceptionHandler() service, you are keeping your code more consistent with the rest of the code (and consistency is always a win).

But, the real benefit of using $exceptionHandler() is that you are passing your error through to a service that has become the de-facto point of override for error logging in the AngularJS community. In my apps, I use the $exceptionHandler() service to gather stack-traces (using Stacktrace.js) and to pass those errors off to New Relic for more aggregated tracking. And, I'm not the only one - a similar approach is also outlined in John Papa's AngularJS Style Guide.

So, long-story-short, favor the $exceptionHandler() unless you truly want to log something without the potentiality of it being logged in a more meaningful way.

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel
Managed ColdFusion hosting services provided by:
xByte Cloud Logo