0

我有以下问题:我试图捕获来自服务器的 HTTP 500 响应,但拦截器没有收到 500 响应。这是由对未经授权的 URL 的 img src 调用生成的(从技术上讲,服务器应该返回 4xx,但它返回 5xx --> 它是第 3 方服务器。

更新 2:如果我执行直接 $http.get,则会调用我的 HTTP 拒绝拦截,但如果 img ng-src 或 img-src 隐式调用 http get,则不会调用它。有趣的是我的 httptimeout 拦截被调用,但响应拦截从未被调用。我该如何解决这个问题?

更新 1:我添加了 500 错误的屏幕截图,我的浏览器通过网络检查器收到但没有被拦截。请看结尾。

拦截器代码:

  .factory('timeoutHttpIntercept', function ($rootScope, $q) {
            //console.log("*** HTTP INTERCEPTOR CALLED ***");
            return {
                'request': function (config) {
                    config.timeout = 15000;
                    //console.log("*** HTTP INTERCEPTOR CALLED ***");
                    return config;
                }


            };
        })

.factory ('httpAuthIntercept', function ($rootScope, $q)
{
    return {
    requestError: function (response) {
      console.log ("**** REJECT REQUEST: "+JSON.stringify(response));
      return $q.reject(response);
    },

    responseError: function (response) {
      console.log ("**** REJECT RESPONSE: "+JSON.stringify(response));
      return $q.reject(response);
    },
    response: function (response)
        {
            console.log("*******RESPONSE with status: "+response.status+"****************");
            if (response.status == 500)
            {
             console.log ("**** RESPONSE: "+JSON.stringify(response));
            }
                return (response);
        }
  };
})

其次,在.config中:

 $httpProvider.interceptors.push('timeoutHttpIntercept');
 $httpProvider.interceptors.push('httpAuthIntercept');

现在,在我的模板中,我调用了以下代码:

<img ng-src="{{LoginData.url}}/cgi-bin/zms?mode=jpeg&amp;monitor={{monitorId}}&maxfps=3&buffer=1000&auth=1234&rand={{rand}}" width="100%" />

以上触发了 500 响应。我看到服务器日志也生成了这个:

Starting capture on eth0 interface
2015-05-11 05:12:44 xx.xx.xx.xx 192.168.1.13    >   GET <server.com>    /cgi-bin/zms?mode=jpeg&monitor=1&maxfps=3&buffer=1000&auth=1234&rand=116426 HTTP/1.1    -
2015-05-11 05:12:44 192.168.1.13    xx.xx.xx.xx <   -   -   -   HTTP/1.1    500 Internal Server Error

但由于某种原因,auth 拦截没有捕获这个 500。如果我在 auth 拦截中删除 500 检查,我可以看到它捕获成功的响应,并显示 200 OK。

我究竟做错了什么?

收到 500 响应屏幕截图 --> Safari 的网络检查器:

收到 500 错误

谢谢

4

2 回答 2

0

您使用的 angularjs 版本是什么?如果它 < 1.3 可能值得尝试将你的拦截器推$httpProvider.responseInterceptors$httpProvider

app.factory('MyHttpInterceptor', function($q) {
        return function (promise) {
            return promise.then(function (response) {
                return response;
            },
            function (response) {
                if (response.status === 500) {
                    // you should hit this for HTTP 500
                }
                return $q.reject(response);
            });
        };
    });


app.config(function ($routeProvider, $httpProvider) {

   // rest of the config code

    $httpProvider.responseInterceptors.push('MyHttpInterceptor');
})

另一种实现

app.factory('MyHttpInterceptor', ['$q', function ( $q) {
    function success(response) {
        // all good
    return response;
    }
    function error(response) {
        var status = response.status;
        if (status == 500) {
            return;
        }
        // otherwise
        return $q.reject(response);
    }
    return function (promise) {
        return promise.then(success, error);
    }
}]);
于 2015-05-11T12:47:53.070 回答
0

I had a similar problem.

My responseError method of the interceptor wasn't triggered after generating an 500 error with Fiddler (proxy). In fiddler you can create your own responses for certain files. The problem somehow was in my own created response. Not sure where the problem was. But after I copied one of the responses allready in Fiddler I got the method responseError working.

Perhaps the response you are getting from the third party server is invalid somehow.

If you want to try with Fiddler. In the programm folder you have certain ResponseTemplates C:\Program Files\Fiddler2\ResponseTemplates

Copied "502_unreachable.dat" to "500_servererror.dat" and only modified the response code to 500 and left response body untouched.

restarted Fiddler and I could apply my new created Fiddler response as auto-responder. And yes it triggered the responseError method.

于 2016-01-21T17:21:28.127 回答