0

I have a factory that has multiple services for querying a custom API I built. The callbacks all work, but I'm wondering how I can have any error handling while fetching.

.factory('customApiService', function ($resource) {
      return {
          yelp: function (businessId, callback) {
              var api = $resource('../../api/Yelp/:businessId', {
                  businessId: businessId
              }, {
                  fetch: 'JSONP',
                  'query': { isArray: false }
              });

              api.fetch(function (response) {
                  callback(response);
              });
          },
          tripAdvisor: function (hotelId, callback) {
              var api = $resource('../../api/TripAdvisor/:hotelId', {
                  hotelId: hotelId
              }, {
                  fetch: 'JSONP',
                  'query': { isArray: false }
              });

              api.fetch(function (response) {
                  callback(response);
              });
          }
      }
  });

And an example of using this factory in a controller:

.controller('yelpCtrl', [
  '$scope', 'customApiService', function ($scope, customApiService) {

      customApiService.yelp("yelpIdHere", function (d) {
          //On successful callback run code
      });

      customApiService.tripAdvisor("tripAdvisorIdHere", function (d) {
          //On successful callback run code
      });
   }
])

Currently if there is any bad response (404, 500, 504, etc), the callback is not fired.

1

2 Answers 2

2

Had to change my Factory to return an errorCallback as well as a successful callback:

.factory('customApiService', function ($resource) {
  return {
      yelp: function (businessId, callback, errorCallback) {
          var api = $resource('../../api/Yelp/:businessId', {
              businessId: businessId
          }, {
              fetch: 'JSONP',
              'query': { isArray: false }
          });

          api.query(function (response) {
              callback(response);
          },
          function (error) {
              errorCallback(error);
          });
      },
      tripAdvisor: function (hotelId, callback, errorCallback) {
          var api = $resource('../../api/TripAdvisor/:hotelId', {
              hotelId: hotelId
          }, {
              fetch: 'JSONP',
              'query': { isArray: false }
          });

          api.query(function (response) {
              callback(response);
          },
          function (error) {
              errorCallback(error);
          });
      }
  }
});

So now in the controller, I have a second function as a parameter to my factory calls which will handle any errors:

.controller('yelpCtrl', [
  '$scope', 'customApiService', function ($scope, customApiService) {

      customApiService.yelp("yelpIdHere", function (d) {
          //On successful callback run code
      },
      function(e){
          //Do error handling here
      });

      customApiService.tripAdvisor("tripAdvisorIdHere", function (d) {
          //On successful callback run code
      },
      function(e){
          //Do error handling here
      });
   }
])

This answer seemed to help

Sign up to request clarification or add additional context in comments.

Comments

1

The solution probably is to use $promise. ngResource instances and collections both have $promise so instead of working with the $resource object use the promise which comes with success, error, finally methods. see $q doc on angular site

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.