Is there an alternative to $httpProvider.responseInterceptors since it has been discontinued in AngularJS V1.3?
The interceptors that were functioning with Angular JS 1.2 are no longer operational in version 1.3
var angularErrorHandling = angular.module('xx-http-error-handling', []);
angularErrorHandling.config(function ($provide, $httpProvider, $compileProvider) {
var elementsList = $();
// add a function to intercept the http responses of the entire application
$httpProvider.interceptors.push(function ($timeout, $q) {
return function (promise) {
return promise.then(function (successResponse) {
// handle successful responses for GET requests
if (successResponse.config.method.toUpperCase() == 'GET') {
var length = successResponse.data.length;
if (length == 0)
{
var countactivetoaster = $('#toast-container').find('.toast').length;
if (countactivetoaster == 0) {
toastr.warning('No Records Found!', '');
}
}
return successResponse;
}
else if (successResponse.config.method.toUpperCase() == 'PUT') {
toastr.success('Data Saved Sucessfully..', '');
return successResponse;
}
else if (successResponse.config.method.toUpperCase() == 'POST') {
toastr.success('Data Saved Sucessfully..', '');
return successResponse;
}
},
// handle unsuccessful responses and display error messages
function (errorResponse) {
switch (errorResponse.status) {
case 400: // Display error for status code 400
toastr.error('400 error.', '');
// Display validation error messages if present
if (errorResponse.data.errors.length > 0) {
for (var i = 0; i < errorResponse.data.errors.length; i++) {
toastr.error('xx-http-error-validation-message', '');
}
}
break;
case 401: // Error message for status code 401
toastr.error('Wrong email address or password!', '');
break;
case 403: // Inform user about authorization denial
toastr.error('You have insufficient privileges to do what you want to do!', '');
break;
case 500: // Internal server error message for status code 500
toastr.error('Error: <br />' +
errorResponse.data.exceptionMessage != null && errorResponse.data.exceptionMessage.length > 0 ? errorResponse.data.exceptionMessage :
errorResponse.data.message, '');
break;
default: // Default error message for other errors
toastr.error('Error ' + errorResponse.status + ': ' + errorResponse.data.message, '');
}
return $q.reject(errorResponse);
});
};
});
$compileProvider.directive('httpErrorMessages', function () {
return {
link: function (scope, element, attrs) {
elementsList.push($(element));
}
};
});
});