FeignException状态为0?

huangapple go评论68阅读模式
英文:

FeignException with status 0?

问题

我在超时问题时收到了一个带有状态码 0 的 FeignException(RetryableException),而不是 HTTP 500 或 504。

在 Postman 中,我收到了 HTTP 状态码 500。

为什么 FeignException 的状态码被设为了 0?

英文:

I'm getting a FeignException(RetryableException) with status 0 instead of HTTP 500 or 504 for timeout problems.

FeignException状态为0?
In Postman I'm getting HTTP status 500.
FeignException状态为0?

Why FeignException is created with status 0?

答案1

得分: 3

基于证据,您的基于Postman的Web应用正在使用Feign尝试与其他某些服务进行通信。当Feign尝试连接到该服务时,要么在连接尝试期间,要么在等待响应期间,会发生超时。

在任何情况下,读取超时意味着Feign无法获取HTTP响应。由于状态代码来自该响应,因此没有HTTP状态代码可放入FeignException中。

您的Postman应用程序代码显然未处理FeignException,因此它会传播到将其(正确地)转换为HTTP 500(内部错误)的Postman堆栈中。

简而言之,Feign没有传递HTTP状态代码,因为它没有获取到状态代码。(而且根本没有一个代表“请求超时”的HTTP状态代码。)


更新

> 我需要处理我的feign客户端的所有异常消息/状态。为此,我对FeignException进行了尝试/捕获。这个异常是由Feign在读取超时超过XXX时间时创建的。我不知道怎么样才能获得postman得到的状态500,而不是状态0。

我认为您正在尝试解决错误的问题。

您想要从不存在的响应中获取HTTP状态。很明显,您无法从不存在的东西中获取内容。或者您期望在这种情况下Feign能够伪造一个响应代码。很明显,它不会这样做。

<sup>正如我在上面解释的那样,Postman提供的500错误不是来自您的Feign请求的响应代码。这是Postman为您的Web应用程序生成的响应代码。在发生超时时,您的代码允许FeignException传播的是一个“内部错误”500错误!</sup>

因此,如果您想要在try/catch中诊断/处理超时,您需要在异常处理程序中专门测试状态是否等于0。然后您可以选择:

  • 将所有状态为零的响应代码视为未诊断的错误。

  • 测试ex.getMessage(),看它是否包含“time out”或类似的内容。(注意,测试异常消息的特定内容可能使您的代码变得脆弱或依赖于平台。但这可能并不是您关心的问题。)

  • 测试ex.getCause(),看它是否指示超时、“连接被拒绝”或其他情况。

英文:

Based on the evidence, your Postman-based webapp is using Feign to try to talk to some other service. When Feign attempts to connect to the service, it is getting a timeout either during the connection attempt, or while waiting for the response.

In either case, the read timeout means that Feign does not get an HTTP response. Since the status code comes from that response, there is therefore no HTTP status code to go into the FeignException.

Your Postman app code is apparently not handling the FeignException, so it is propagating into the Postman stack which is converting it (correctly) into a HTTP 500 (Internal Error).

In short, Feign is not passing on an HTTP status code because it doesn't get one. (And there isn't an HTTP status code that means "request timed out" anyway.)


UPDATE

> I need to process all exception message/status of my feign client. For that, i put a try/catch for FeignException. This exception is created by feign when readtimeout is over XXX time. I don't know how i can get the status 500 got by postman instead of 0.

I think that you are trying to solve the wrong problem.

You are wanting to get an HTTP status from a response when the response does not exist. Clearly you can't get something that doesn't exist. Alternatively you are expecting feign to fake a response code in this case. Clearly it doesn't.

<sup>And as I explained above, the 500 error that Postman is giving is not the response code from your feign request. It is a response code that Postman is generating for your web app. It is an "internal error" that your code is allowing the FeignException to propagate in the case that there is a timeout!</sup>

So if you want to diagnose / handle timeouts in the try / catch you need to specifically test for the status == 0 in the exception handler. Then you could either:

  • Treat all cases with a zero response code as an undiagnosed error.

  • Test ex.getMessage() to see if it contains "time out" or "timed out" or similar. (Beware that testing the specific content of an exception message makes your code potentially fragile or platform dependent. But that may not be a concern for you.)

  • Test ex.getCause() to see if the cause exception indicates a timeout, "connection refused" or something else.

huangapple
  • 本文由 发表于 2020年9月4日 22:55:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63743555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定