Search code examples
javafeign

FeignException with status 0?


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

enter image description here In Postman I'm getting HTTP status 500. enter image description here

Why FeignException is created with status 0?


Solution

  • 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.

    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!

    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.