Search code examples
angularjsngresource

AngularJS $resource Error Handing with Callback


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.


Solution

  • 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