Angular2 rxjs http.request.catch对于某些http错误有奇怪的行为 [英] Angular2 rxjs http.request.catch has strange behaviour for some http errors

查看:199
本文介绍了Angular2 rxjs http.request.catch对于某些http错误有奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的http服务没有正确抓住一些http错误。
catch方法有两个不同的响应对象(见下文)。

My http service does not catch correctly some http errors. The catch method has 2 different response objects ( see below ).

private fireRequest(request: Request): Observable<any> {
    return this.http.request(request)
    .switchMap((response: Response) => {
        const responseData = response.json() || [];
        return Observable.of(responseData);
    })
    .catch((response: Response) => {
        const res2 = response.json();
        // filters http errors from status 0 errors
        if (response.status && response.status > 0) {
            const res = response.json();
            return Observable.of<any>(res);
        }
        const unexpectedNetworkError = new 
              Error('commons.unexpected_network');
        return Observable.throw(unexpectedNetworkError);
    })
}

奇怪的错误行为。
(即使在Chrome网络标签页我看不到http身体)

Strange error behaviour. ( even in chrome network tab i do not see the http body )

// catch 404 error
{ 
  headers: Headers,
  ok: false,
  status: 0,
  statusText : "",
  type : 3,
  url : null,
  _body : ProgressEvent
}

正确的错误行为

// catch 401 error
{ 
  headers: Headers,
  ok: false,
  status: 401,
  statusText : "Unauthorized",
  type : 2,
  url : http://api.service/users,
  _body : { // json body}
}


推荐答案

我发现问题浏览器及其CORS行为。

I found out the problem regards the browser and its CORS behaviour.

a)Succefull调用

BROWSER                  SERVER
    | ------ preflight ---> |
    | <-- allow GET, PUT -- |
    | ------- GET req ----> |
    | <------ GET res ----- |

b)支持响应404而不是200 >

b) Failed call with backed response 404 instead of 200

BROWSER                SERVER
    | ---- preflight ----> |
    | <------ 404 -------- |
    |                      |
    |                      |

c)没有intenet连接,服务器没有响应

BROWSER                SERVER
    | ---- preflight ----> X
    |                      |
    |                      |
    |                      |

每当您对非现有的ruote进行api调用时,浏览器会发送一个预检请求(b)

Whenever you make an api call to a non existing ruote, the browser sends a preflight request (b).

根据您可以收到的后端错误处理策略:

Depening on your backend error handling strategy you could receive:

1) Http 404代码,其中包含身体中的错误详细信息
在这种情况下,浏览器会返回带有http状态代码0的通用错误,而不需要实际的服务器负载。第二个呼叫是从来没有做过的,因为它没有任何意义,因为预检错误。

1) Http 404 code with error details in the body In this case the browser returns a generic error with http status code 0 without the actual server's payload. The second call is never made because there is no sense in doing it since the preflight went wrong.

2) Http 200代码为预检404用于实际通话。
在这种情况下,一切正常。

2) Http 200 code for the preflight and 404 for the actual call. In this case everything works fine.

如果没有互联网连接,问题依然存在。

The problem remains if there is no internet connection.

这篇关于Angular2 rxjs http.request.catch对于某些http错误有奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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