在角$ HTTP和拒绝承诺截获服务器错误 [英] Intercepting server errors in Angular $http and rejecting promise

查看:124
本文介绍了在角$ HTTP和拒绝承诺截获服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的角度HTTP服务,并拦截来自服务器的响应拒绝承诺之前发现服务器错误,并做一些东西与他们(日志等)。

顺便说一句,我有时会得到像验证错误回即使状态code响应的身体是200,我知道这是不理想,但请不要误会挂在那,因为它不是我的问题的原因。我以为我会提到它,虽然它解释了为什么我在拦截响应和responseError

  $ httpProvider.interceptors.push(函数($日志$ rootScope,$ Q){    返回{
        回应:功能(响应){            如果(response.data&安培;&安培; response.data.success ===假){
                doSomeStuff(响应);
                返回$ q.reject(响应);
            }
            其他{
                返回响应;
            }        },
        responseError:函数(拒绝){
            doSomeStuff(拒绝);
            $ q.reject(拒绝);
        }    };});

然后,在我的应用程序一个典型的HTTP调用可能是这样的。

  $ http.post('https://domain.com/api/whatever',数据)。然后(功能(响应){
    的console.log(时会返回错误不要这样做)
});

不幸的是,执行console.log始终运行,即使存在HTTP错误,或具有嵌入误差的200状态。

我敢肯定,我只是误解$ HTTP / $ Q如何在这里工作,有人就能把我直。


解决方案

  $ q.reject(拒绝);

返回一个新的拒绝承诺,它不会影响现有承诺的状态。事实上,所有的 responseError 这里确实是抓住了拒绝,并返回未定义响应来代替。

这应该是

  responseError:功能(拒绝){
        doSomeStuff(拒绝);
        返回$ q.reject(拒绝);
    }

I'm using the Angular HTTP service and intercepting the response from the server to catch any server errors and do some stuff with them (logging etc.) before rejecting the promise.

As an aside, I sometimes get things like validation errors back in the body of the response even though the status code is 200. I know that's not ideal but please don't get hung up on that as it's not the cause of my problem. I thought I'd mention it though as it explains why I'm intercepting response and responseError

$httpProvider.interceptors.push(function($log, $rootScope, $q) {

    return {
        response: function(response) {

            if (response.data && response.data.success === false) {
                doSomeStuff(response);
                return $q.reject(response);
            }
            else{
                return response;
            }

        },
        responseError: function(rejection) {
            doSomeStuff(rejection);
            $q.reject(rejection);
        }

    };

});

Then, a typical HTTP call in my app might look like this.

$http.post('https://domain.com/api/whatever', data).then(function (response) {
    console.log("Don't do this when an error is returned")
});

Unfortunately, the console.log always runs, even if there is an HTTP error, or a 200 status with embedded errors.

I'm sure I'm just misunderstanding how $http/$q works here and somebody will be able to put me straight.

解决方案

$q.reject(rejection);

returns a new rejected promise, it doesn't affect the state of existing promise. In fact, all that responseError does here is catches the rejection and returns undefined response instead.

It should be

    responseError: function(rejection) {
        doSomeStuff(rejection);
        return $q.reject(rejection);
    }

这篇关于在角$ HTTP和拒绝承诺截获服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆