3

I would like to intercept 500 error and higher.

I get inspired by:

this is the ServiceErrorInterceptor

app.factory('ServerErrorInterceptor',
[ '$q',
  function ($q) {
    return {
      'requestError': function(rejection) {
        console.log( rejection );
         return $q.reject(rejection);
      },
        responseError: function (rejection) {
            console.log( rejection );
           return $q.reject(rejection);
        }
      };
    }
  ]);

which always returns

Object { data: null, status: -1, headers: headersGetter/<(), config: Object, statusText: "", xhrStatus: "error" }

while [Network] bowser tab shows clearly 400, 405, 500, 503 ... depending of the case.

How can I get the http error code?

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
mickro
  • 881
  • 2
  • 11
  • 26
  • 1
    `-1` usually means that some problem took place, like CORS or timeout. Can you replicate the problem as a plunk or something? – Estus Flask Oct 28 '17 at 19:27
  • try to intercept response too ... it helped us to get the HTTP code for errors and you need to use if switch in the callback method – michal.jakubeczy Oct 28 '17 at 20:40
  • thanks @estus I finally opted to isolate the server part by a local PHP page which is by setting headers only. And YES setting `header('Access-Control-Allow-Origin: *');` allows Angular to read the status. Which is not fun when you do not have control on the real server. – mickro Oct 28 '17 at 21:55
  • @jabo87 sure I inspected `response` and `request` but that have not been relevant here. – mickro Oct 28 '17 at 21:57
  • When you have multidomain setup you should be prepared for this. And if you don't have control over API server headers, you have to resort to CORS proxy. Not specific to Angular but to client-side JS in whole. – Estus Flask Oct 28 '17 at 22:01

0 Answers0