title: refactor angular ng-resource error handling
body: <p>Warning: coffeescript ahead.</p>
  
  <p>I have multiple controllers with various ngresource functions like so:</p>
  
  <pre><code>app.controller 'usersCtrl', ($scope, $state, $filter, $flash, $api) -&gt;
  
  $scope.saveUser = -&gt;
      $scope.spinner = true
      $api.User.save($scope.user).$promise.then ((response) -&gt;
         ....
      ), (error)
         $scope.spinner = false
         if error.code = 400
            $flash.create('warning', error.message)
         else if error.code = 500
            $flash.create('danger', error.message)
  
  $scope.updateFoo = -&gt;
      $scope.spinner = true
      $api.Foo.update(foo: $scope.foo).$promise.then ((response) -&gt;
         ....
      ), (error) -&gt;
         $scope.spinner = false
         if error.code = 400
            $flash.create('warning', error.message)
         else if error.code = 500
            $flash.create('danger', error.message)
  </code></pre>
  
  <p>Given that I'm using virtually the same error handling code across dozens of functions, how do I refactor the code so I can reuse across multiple functions.</p>