Service Function
myService.serviceName = function (userId) {
return $http({
method: 'POST',
url: '/someUrl'
}).then(function successCallback(response) {
return response.data;
}, function errorCallback(response) {
console.log('An error occurred in the service');
console.log(response);
});
};
Controller Function
myService.ControllerName(data.id)
.then(function successCallback(data) {
// Perform operations here
}, function errorCallback(response) {
toaster.pop({
type: 'error',
title: 'Display Error Message!'
});
});
Within the service, we are detecting error status codes -1
and -2
and based on these codes, we are showing custom error messages to the user.
- Question: How can I transmit error messages or codes from the service to the controller?