title: Recommended practice for application exception handling in AngularJS
body: <p>I am currently exploring possible methods to handle application-wide exceptions in AngularJS.</p>
  
  <p>One of the things we really wanted to avoid was wrapping multiple parts of the application in nested try/catch blocks, but handle things cleanly - i.e throw an exception in response to a promise.</p>
  
  <ul>
  <li>Has anyone covered this issue before and have any recommendations?</li>
  <li>Any suggestions on how to pick up exceptions in services as well as controllers/directives. (See below - broadcast works ok, but only if you can attach a listener to a scope).</li>
  </ul>
  
  <p><strong>Progress so far</strong></p>
  
  <p>A few short design goals:</p>
  
  <ul>
  <li>Allow exceptions from one part of the application to be handled elsewhere - or possibly multiple places (i.e. 'display error notification to user', 'disable widget').</li>
  <li>Provide central management of common error conditions - i.e. log to server, display notification to user, redirect to login.</li>
  <li>Allow exceptions to be thrown from controllers, directives, services etc.</li>
  <li>Eventually allow localized messages.</li>
  </ul>
  
  <p>The current leaning of my team is to write a service to handle exceptions, which would expose a range of simple calls:</p>
  
  <pre><code>exceptionService.warn('exception_token');
  
  exceptionService.crit('another_exception_token');
  </code></pre>
  
  <p>This service would then format an 'exception' object and broadcast this from the rootscope. This would allow a default handler to watch for any broadcasts and apply default actions, as well as allow custom listeners to be set in others scopes, which could handle more specific conditions - i.e. disable a part of the UI.</p>
  
  <pre><code>var exception = {
      token: 'exception_token',
      severity': 'crit'
  };
  
  // broadcast exception
  $rootScope.$broadcast(
  'application_exception',
      exception
  );
  </code></pre>