From d286c4221330adc010e408c3edf4c2d44def39f8 Mon Sep 17 00:00:00 2001 From: sathish Ramesh Date: Fri, 13 Dec 2024 16:22:36 +0530 Subject: [PATCH] fix: handle external timeout error with status code 531 --- .../com/mx/path/connect/http/HttpClientFilter.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/http/src/main/java/com/mx/path/connect/http/HttpClientFilter.java b/http/src/main/java/com/mx/path/connect/http/HttpClientFilter.java index 27792214..94c97ecf 100644 --- a/http/src/main/java/com/mx/path/connect/http/HttpClientFilter.java +++ b/http/src/main/java/com/mx/path/connect/http/HttpClientFilter.java @@ -3,6 +3,7 @@ import java.io.EOFException; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.net.SocketTimeoutException; import java.net.URISyntaxException; import java.time.LocalDate; import java.time.LocalDateTime; @@ -35,6 +36,7 @@ import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; +import org.apache.http.NoHttpResponseException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.config.CookieSpecs; @@ -42,6 +44,7 @@ import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.HttpHostConnectException; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.entity.ByteArrayEntity; @@ -65,6 +68,7 @@ public class HttpClientFilter extends RequestFilterBase { * instead of converting it to a String. */ private static final List RAW_BODY_CONTENT_TYPE_HINTS = Arrays.asList("image", "pdf", "msword"); + private static final int HTTP_STATUS_EXTERNAL_TIMEOUT = 531; private static GsonBuilder gsonBuilder = new GsonBuilder(); private static final Gson GSON = gsonBuilder @@ -157,6 +161,15 @@ public final void execute(Request request, Response response) { } finally { response.finish(); } + } catch (ConnectTimeoutException e) { + httpResponse.setStatus(HttpStatus.valueOf(HTTP_STATUS_EXTERNAL_TIMEOUT)); + throw new ConnectException("Connection timeout: " + e.getMessage(), e); + } catch (SocketTimeoutException e) { + httpResponse.setStatus(HttpStatus.valueOf(HTTP_STATUS_EXTERNAL_TIMEOUT)); + throw new ConnectException("Read timeout: " + e.getMessage(), e); + } catch (NoHttpResponseException e) { + httpResponse.setStatus(HttpStatus.valueOf(HTTP_STATUS_EXTERNAL_TIMEOUT)); + throw new ConnectException("Target server failed to respond: " + e.getMessage(), e); } catch (SSLHandshakeException e) { throw new HttpClientConnectException("SSL handshake failed", e); } catch (SSLException e) {