'HTTP 504 error - program not getting error back

I am calling a 3rd party API. The behavior of my program is as follows:

  1. HTTP Call is made
  2. Program waits for 15 minutes
  3. Code lands in my exception handler with a "Connection timed out (Read failed)" error
  4. Problem is intermitted, it happens about 20% of the time

If I look at my Elastic logs, I can see that we got a 504 "Gateway Timeout" error about 20 seconds after the HTTP call was made.

Why is it that my program does not get the 504 error, but instead waits 15 minutes and gets a timeout? I have called many APIs in the past and I always get the HTTP error back in the HTTP response. Why don't I see the same behavior in this code?

My application's log - HTTP call was made: enter image description here

Elastic Log: enter image description here

My application's log - 15 minutes after call was made: enter image description here

Code:

try {
    this.LOGGER.info("Before httpClient.execute");
    CloseableHttpResponse httpResponse = HttpClients.createDefault().execute(httpPost);
    this.LOGGER.info("After httpClient.execute");
    int status = httpResponse.getStatusLine().getStatusCode();
    String responseString = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
    JSONObject responseJson = new JSONObject(responseString);
    String reason = httpResponse.getStatusLine().getReasonPhrase();
    this.LOGGER.info("HostAppointmentResponse makeAppointment response: " + responseString);
    return HostAppointmentResponse.builder().status(status).responseJson(responseJson).reason(reason).build();
}
catch (Throwable e) {
    this.LOGGER.error("Exception: " + e.getMessage());
    return HostAppointmentResponse.builder().status(400).responseJson(null).reason(e.toString()).build();
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source