0

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.

2
  • We need to see the implementation of UserService.api.getProfile() before we could tell, but my guess is that there is a third parameter which takes the failure callback function. Commented Aug 11, 2021 at 20:27
  • Hi @JacobStamm I have included getProfile above. Commented Aug 12, 2021 at 4:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It works!!! thank you so much, this is very helpful. Will understand further on interceptors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.