HttpClient错误处理困难

HttpClient错误处理困难

本文介绍了Angular HttpClient错误处理困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新HttpClient上的Angular文档 https://angular.io/guide/http 有一个获取错误详细信息"部分,其中显示了如下示例.我已修改注释以记录我的观察结果,哪些基本错误类最终出现在哪里.

The Angular documentation on the new HttpClienthttps://angular.io/guide/httphas a section "Getting error details" where they show an example like below. I have modified to comments to document my observations which basic error classes end up where.

http
.get<ItemsResponse>('/api/items')
.subscribe(
  data => {...},
  (err: HttpErrorResponse) => {
    if (err.error instanceof Error) {
      // we never seem to get here
      console.log('An error occurred:', err.error.message);
    } else {
      // no network connection, HTTP404, HTTP500, HTTP200 & invalid JSON
      console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
    }
  }
);

因此,至少3种完全不同的错误类别(网络,http,响应解析)出现在单个通道上,对于每种类型,必须在HttpErrorResponse的另一部分中搜索实际原因.

So at least 3 totally different categories of errors (network, http, response parsing) come in on a single channel and for each type one has to search for the actual reason in another part of HttpErrorResponse.

一个人只能通过标准错误代码识别一个类别,即HTTP错误.但是其他两种类型?

One can identify only one category, the HTTP errors through the standard error codes. But the other two types?

拔出网络插头后,我会非常清楚地看到err.message =对(未知URL)的HTTP故障响应:0未知错误.非常具有描述性.

When I pull my network plug I get the extremely telling err.message = "Http failure response for (unknown url): 0 Unknown Error." Very descriptive.

当新的内部JSON解析失败时,它表示存在语法错误-而不是在什么位置/字符(由JSON.parse()返回).另请参见此处

When the new internal JSON parsing fails it says that there is a syntax error - but not at what position/character (as returned by JSON.parse()). See also here

超时等其他错误呢?

在哪里可以找到有关此文件的文档?有没有人想出一种可以对此进行一般分析的方法,以便可以向用户提供有意义的消息?

Where can I find documentation on this? Has anyone figured out a way to generally analyse this so one can give meaningful messages to the user?

Chrome on Win,后端上的Angular 5.2:ASP.NET4.6.1纯IHttpHandler

Angular 5.2 in Chrome on Win, Backend: ASP.NET4.6.1 pure IHttpHandler

推荐答案

我必须查看角度源代码以了解可能的错误情况.错误对象将始终为HttpErrorResponse,但其error属性可能会根据情况而有所不同.当使用默认的XHRBackend和默认的responseType'json'时,会发生以下情况:

I had to look at the Angular source code to understand the possible error conditions. The error object will always be a HttpErrorResponse, but its error property may differ depending on the situation. Here's what can happen when using the default XHRBackend with the default responseType of 'json':

收到的HTTP状态代码指示错误(不是200-299)的响应:

HttpErrorResponse.error将被解析为JSON ,如果解析失败,则将其解析为文本.

HttpErrorResponse.error will be the body parsed as JSON or if parsing fails, the body as text.

收到的响应带有良好的HTTP状态代码(200-299),但是正文无法解析为JSON:

HttpErrorResponse.error将是具有以下两个属性的类型为HttpJsonParseError的对象:

HttpErrorResponse.error will be an object typed as HttpJsonParseError with two properties:

  • error:解析错误(类型为Error)
  • text:正文文本
  • error: the parse error (of type Error)
  • text: the body text

由于XHR网络错误而未收到响应:

HttpErrorResponse.error将是XHR ProgressEvent,其类型设置为'error'. (在较早版本的TypeScript中,它可能被错误地键入为ErrorEvent.)

HttpErrorResponse.error will be an XHR ProgressEvent with the type set to 'error'. (It may be wrongly typed as an ErrorEvent in older versions of TypeScript.)

注意:Angular在发出请求时不会设置超时,也不会注册超时事件处理程序.默认XHR超时为0,因此请求永远不会在客户端超时.

Note: Angular does not set a timeout when making requests, nor does it register a timeout event handler. The default XHR timeout is 0, so requests will never time out client-side.

这篇关于Angular HttpClient错误处理困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:33