Skip to content

Commit cfa0d34

Browse files
author
sathish Ramesh
committed
fix: handle external timeout error with status code 531
1 parent 4cf52c9 commit cfa0d34

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

http/src/main/java/com/mx/path/connect/http/HttpClientFilter.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.apache.http.client.entity.UrlEncodedFormEntity;
4545
import org.apache.http.client.methods.RequestBuilder;
4646
import org.apache.http.client.utils.URIBuilder;
47-
import org.apache.http.conn.ConnectTimeoutException;
4847
import org.apache.http.conn.HttpHostConnectException;
4948
import org.apache.http.conn.ssl.NoopHostnameVerifier;
5049
import org.apache.http.entity.ByteArrayEntity;
@@ -68,7 +67,6 @@ public class HttpClientFilter extends RequestFilterBase {
6867
* instead of converting it to a String.
6968
*/
7069
private static final List<String> RAW_BODY_CONTENT_TYPE_HINTS = Arrays.asList("image", "pdf", "msword");
71-
private static final int HTTP_STATUS_EXTERNAL_TIMEOUT = 531;
7270

7371
private static GsonBuilder gsonBuilder = new GsonBuilder();
7472
private static final Gson GSON = gsonBuilder
@@ -161,15 +159,12 @@ public final void execute(Request request, Response response) {
161159
} finally {
162160
response.finish();
163161
}
164-
} catch (ConnectTimeoutException e) {
165-
httpResponse.setStatus(HttpStatus.valueOf(HTTP_STATUS_EXTERNAL_TIMEOUT));
166-
throw new ConnectException("Connection timeout: " + e.getMessage(), e);
162+
} catch (ConnectException e) {
163+
throw new HttpClientConnectException("Connection Exception", e);
167164
} catch (SocketTimeoutException e) {
168-
httpResponse.setStatus(HttpStatus.valueOf(HTTP_STATUS_EXTERNAL_TIMEOUT));
169-
throw new ConnectException("Read timeout: " + e.getMessage(), e);
165+
throw new HttpClientConnectException("Read Timeout Exception", e);
170166
} catch (NoHttpResponseException e) {
171-
httpResponse.setStatus(HttpStatus.valueOf(HTTP_STATUS_EXTERNAL_TIMEOUT));
172-
throw new ConnectException("Target server failed to respond: " + e.getMessage(), e);
167+
throw new HttpClientConnectException("Target server failed to response", e);
173168
} catch (SSLHandshakeException e) {
174169
throw new HttpClientConnectException("SSL handshake failed", e);
175170
} catch (SSLException e) {

http/src/test/groovy/com/mx/path/api/connect/http/HttpClientFilterTest.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.mx.path.connect.http.HttpClientFilter
88
import com.mx.path.connect.http.HttpRequest
99
import com.mx.path.connect.http.HttpResponse
1010
import com.mx.path.core.common.collection.MultiValueMap
11+
import com.mx.path.core.common.connect.ConnectException
1112
import com.mx.path.core.common.connect.Request
1213
import com.mx.path.core.common.connect.RequestFilter
1314

@@ -111,4 +112,22 @@ class HttpClientFilterTest extends Specification {
111112
false || makeContentTypeHeaders("application/xml")
112113
false || makeContentTypeHeaders()
113114
}
115+
116+
def "test connect exception handling in execute method"() {
117+
given: "A mock HttpRequest and Response"
118+
def mockRequest = Mock(HttpRequest)
119+
def mockResponse = Mock(HttpResponse)
120+
121+
// Create the real executor object (HttpClientFilter)
122+
HttpClientFilter executor = Mock(HttpClientFilter)
123+
124+
executor.execute(mockRequest, mockResponse) >> { throw new ConnectException("Connection Exception") }
125+
126+
when: "Execute method is called"
127+
executor.execute(mockRequest, mockResponse)
128+
129+
then: "ConnectException is thrown"
130+
thrown(ConnectException)
131+
}
132+
114133
}

0 commit comments

Comments
 (0)