Issue
Below is the code
UserService.api.getProfile({userLogin: $scope.login}, function(res) {
_.each($scope.visibility, function(e) {
e.visible = (_.find(res, { 'roleName': e.name })) ? true : false;
});
});
And for userService.api.getProfile will be as below
angular.module('frontendApp').factory('UserService', [
'$resource',
function($resource) {
return {
api: $resource('service/authority-roles/:roleId', {}, {
getProfile: {
method: 'GET',
url: 'service/user/:userLogin/authority-roles',
isArray: true
}
It works well if the API is sending response 200. However, when the API returns the response 404, the code unable to catch the status.
I tried using "Try" and "Catch" but it's not working. I also did console.log on res.status to get the status code but the value is undefined.
The plan is, if the API returning 400 response, then alert "User Not Found" message.
Appreciate your help on this. Thanks in advance.
Solution
Assuming you don't have any global $http
interceptors that will handle this, you can do this on a per-api method basis by specifying an interceptor
object on your getProfile
action, like so:
getProfile: {
method: 'GET',
url: 'service/user/:userLogin/authority-roles',
isArray: true,
interceptor: {
responseError: function(rejection) {
// do something on error
return $q.reject(rejection);
}
}
}
You can read more about interceptors here.
Answered By - Jacob Stamm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.