Angular $http 在 404 上调用成功

Angular $http calling success on 404

我正在通过服务公开此功能:

function getData(url) {
    return $http.get(url).then(function (response) {
        return response.data;
    });
}

如果调用成功,一切都很好 - 我得到了一个按预期解决的承诺。

如果我传入生成 404 的 URL,我得到:

TypeError: Cannot read property 'data' of undefined

上线:return response.data;

我可以在 Chrome 的开发者工具中看到 GET returns 404.

为什么 Angular(v1.4.7,也尝试使用 v1.5.0)调用我的 successCallback undefined 出错?

(我想做的是处理调用代码中的失败。)

编辑: @jcaron 为我指出了正确的方向。问题似乎是此配置行(由另一位开发人员编写):

$httpProvider.interceptors.push('ErrorInterceptor');

拦截器肯定有设计缺陷:

function ErrorInterceptor($q, $window) {
    return {
        response: function (response) {
            return response;
        },
        responseError: function (response) {
            if (response.status === 500) {
                $window.location.href = '/error';
                return $q.reject(response);
            }
        }
    };

The interceptor must have a design flaw

确实有。它 returns undefined 在拒绝不是 500 状态的情况下,它履行了承诺。应该是

…
responseError: function (err) {
    if (err.status === 500) {
        $window.location.href = '/error';
    }
    return $q.reject(err); // always pass on rejection
}