0

I am trying to catch server error for eg 500 in my angular app. Unfortunately this construction fails:

            return promise = this.httpService.jsonp("serverurl")
            .success((response: any): ng.IPromise<any> => { return response.data; })
            .error((response: any): ng.IPromise<any> => { return response.data; });

I want to catch server response - in this case simply the message. How to do this?

1 Answer 1

1

The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise.

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
    console.log(response); // add console log of response
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    console.log(response); // add console log of error response
  });

Or a interceptor can be used to "monitor" all http's:

// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
  return {
    // optional method
    'request': function(config) {
      // do something on success
      return config;
    },

    // optional method
   'requestError': function(rejection) {
      // do something on error
      if (response.status === 500) {
          //DO WHAT YOU WANT
      }   
      return $q.reject(rejection);
    },



    // optional method
    'response': function(response) {
      // do something on success
      return response;
    },

    // optional method
   'responseError': function(rejection) {
      // do something on error
      if (response.status === 500) {
          //DO WHAT YOU WANT
      }                
      return $q.reject(rejection);
    }
  };
});

$httpProvider.interceptors.push('myHttpInterceptor');
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you this caused the proper window to appear on error. Is there a way to protect server error from showing in the web browser console window?
In the example i've updated 'response.status === 500' on the responseError. In the first example you are console.logging the error. Or about what console error are you talking about? :)
I mean that I can properly show to user proper message on 500 Error now. But if somebody starts console there is: "NetworkError: 500 Internal Server Error - " and the url of server. This is not crucial but if it is possible to prevent from showing this message in the console I would like to do so.
It's a browser specific behaviour. But have a look at following : stackoverflow.com/questions/14337351/…

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.