0

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.. https://github.com/johnpapa/generator-hottowel/blob/master/app/templates/src/client/app/blocks/exception/exception.js

Here

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

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

Xyt
  • 21
  • 9
  • I'm not sure what the end result you're after is. Can you elaborate with an example? – Phix Sep 11 '16 at 02:18

1 Answers1

1

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')
    .then(success)
    .catch(fail);

  ...

  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')
    .then(success)
    .catch(exception.catcher('XHR Failed for getPeople'));
Community
  • 1
  • 1
PerfectPixel
  • 1,918
  • 1
  • 17
  • 18