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?

cAMPy
  • 567
  • 2
  • 8
  • 25

1 Answers1

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');
daan.desmedt
  • 3,752
  • 1
  • 19
  • 33
  • 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? – cAMPy Jul 20 '16 at 08:11
  • 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? :) – daan.desmedt Jul 20 '16 at 08:18
  • 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. – cAMPy Jul 20 '16 at 08:36
  • It's a browser specific behaviour. But have a look at following : http://stackoverflow.com/questions/14337351/can-i-prevent-the-chrome-developer-tools-console-from-logging-image-404-errors/14427545#14427545 – daan.desmedt Jul 20 '16 at 08:39