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.