Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-app="app">
  <div ng-controller="ctrl as ctrl">
    <button ng-click="callErr()">Call Error</button>
    <p>
    Error: "{{ error }}"
    Error Message: "{{ errorMsg }}"
    </p>
  </div>
</body>
</html>
 
angular
  .module('app', [])
  .factory('errors', function ($rootScope) {
    function broadcast (error) {
      $rootScope.$broadcast('err:'+error.type, error.message, error.error);
    }
    return {
      catch: function (type, message) {
        return function (error) {
          broadcast({
            type: type,
            message: message,
            error: error
          });
        };
      }
    };
  })
  .controller('ctrl', function($scope, $q, errors){
    $scope.$on('err:test', function(e, msg, err){
      $scope.error = err;
      $scope.errorMsg = msg;
    });
    $scope.callErr = function () {
      $q.reject('A test error.')
      .catch(errors.catch('test', 'A test error message'));
    };
  });
Output 300px

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers