Getting server response in angular response interceptorError
I just built an interceptor service in angularJS to catch all errors from API calls to handle common errors like:
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
return {
'responseError': function(rejection) {
alert("Something went wrong");
return $q.reject(rejection);
}
};
});
Works perfectly and my server is sending this error message with the status 409
{
message: "Email is already being used"
success: false
token: ""
}
How do I access this answer from the interceptor responseError
?
+3
denislexic
source
to share
2 answers
it can be done like this:
$httpProvider.interceptors.push(['$q', function($q) {
return {
'request': function (config) {
//request codes
return config;
},
'responseError': function(response) {
console.log(response);
if(response.statusText){
alert(response.statusText)
}else{
alert("Server down")
}
if(response.status === 401 || response.status === 409) {
//response code
}
return $q.reject(response);
}
};
}]);
})
+2
Samundra khatri
source
to share
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
return {
'responseError': function(rejection) {
if(rejection.status === 409) {
//get set the error message from rejection.message/rejection.data.message and do what you want
}
alert("Something went wrong");
return $q.reject(rejection);
}
};
});
+1
jrath
source
to share