Xyt Xyt - 1 year ago 91
AngularJS Question

About Johnpapa AngularJS exception catcher

I've read Johnpapa AngularJS styleguide, it's really interesting.
But, there is something I didn't understand, it's the exception catcher factory, can someone give me an example code..


function catcher(message) { return function(e) {...}; }

How can we provide both message and promise (e) arguments?
Sorry for my bad english, I'm french ^^

Answer Source

It is just an abstraction so you do not have to throw exceptions at every single point in your app. If you would do that and decide to change the way your exception messages are printed, you would have to change many places in your app. With this factory, you change it in one place.

catcher is a closure (read here what a closure is). It is basically a function that returns a function that you can then call with another value. Think of it as you are preparing the catcher with your message and later plug in the value that is supposed to be reported as well. In his data service you see him use the catcher as follows:

function getPeople() {
  return $http.get('/api/people')


  function fail(e) {
    return exception.catcher('XHR Failed for getPeople')(e);

Utilizing the closure, you could do this as well:

function getPeople() {
  return $http.get('/api/people')
    .catch(exception.catcher('XHR Failed for getPeople'));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download