Service.js
myService.serviceName = function (userId) {
return $http({
method: 'POST',
url: '/someUrl'
}).then(function successCallback(response) {
return response.data;
}, function errorCallback(response) {
console.log('Service errorCallback');
console.log(response);
});
};
Controller.js
myService.ControllerName(data.id)
.then(function successCallback(data) {
//do processing here
}, function errorCallback(response) {
toaster.pop({
type: 'error',
title: 'Display Error Message!'
});
});
In service, we are getting error status code in console viz -1, -2 and based on that code we are displaying custom error message to the user.
- How do I pass error (message/code) from service to controller ?
.catch(function (error) { throw error; }), because.catch(function (error) { toaster.pop({ type: 'error', title: 'Display Error Message!' }); })can catch all http errors.Service - statusCode="-1" Controller - Error: data is undefinedProbably, in controller it goes intotheninstead ofcatch