handled connection timeout exception

This commit is contained in:
Lincpay Tech
2025-12-11 18:00:36 +05:30
parent 6079ce48a3
commit b3c507338e
2 changed files with 117 additions and 3 deletions
@@ -2,6 +2,7 @@ package com.billpaymentservice.bbps.serviceImpl;
import java.util.HashMap;
import java.net.ConnectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -12,6 +13,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.ResourceAccessException;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -24,6 +26,15 @@ public class HttpService {
private final RestClient restClient;
private final ObjectMapper objectMapper = new ObjectMapper();
// Helper to produce server-down/timeout style responses
private HashMap<String, String> buildServerDownResponse(String code, String message, Exception e) {
HashMap<String, String> response = new HashMap<>();
response.put("respCode", code);
response.put("data", message);
log.error("❌ {} - {}", code, message, e);
return response;
}
public HashMap<String, String> postRequest(String url, String payload, HttpHeaders customHeaders) {
@@ -60,6 +71,15 @@ public class HttpService {
response.put("respCode", "403");
response.put("data", "FORBIDDEN - Check credentials in HttpService");
} catch (ResourceAccessException e) {
// This often wraps timeouts or connection refused
Throwable cause = e.getCause();
if (cause instanceof ConnectException) {
return buildServerDownResponse("503", "Server unavailable - connection refused", e);
} else {
return buildServerDownResponse("503", "Server unreachable: " + e.getMessage(), e);
}
} catch (Exception e) {
log.error("❌ HttpService Exception: {}", e.getMessage());
response.put("respCode", "500");
@@ -97,6 +117,13 @@ public class HttpService {
log.error("❌ HTTP Error: {}", httpException.getMessage(), httpException);
response.put("respCode", String.valueOf(httpException.getStatusCode().value()));
response.put("data", httpException.getResponseBodyAsString());
} catch (ResourceAccessException e) {
Throwable cause = e.getCause();
if (cause instanceof ConnectException) {
return buildServerDownResponse("503", "Server unavailable - connection refused", e);
} else {
return buildServerDownResponse("503", "Server unreachable: " + e.getMessage(), e);
}
} catch (Exception e) {
log.error("❌ Exception during GET call: {}", e.getMessage(), e);
response.put("respCode", "500");
@@ -133,6 +160,13 @@ public class HttpService {
log.error("❌ HTTP Error: {}", httpException.getMessage(), httpException);
response.put("respCode", String.valueOf(httpException.getStatusCode().value()));
response.put("data", httpException.getResponseBodyAsString());
} catch (ResourceAccessException e) {
Throwable cause = e.getCause();
if (cause instanceof ConnectException) {
return buildServerDownResponse("503", "Server unavailable - connection refused", e);
} else {
return buildServerDownResponse("503", "Server unreachable: " + e.getMessage(), e);
}
} catch (Exception e) {
log.error("❌ Exception during GET call: {}", e.getMessage(), e);
response.put("respCode", "500");
@@ -171,6 +205,13 @@ public class HttpService {
response.put("respCode", String.valueOf(e.getStatusCode().value()));
response.put("data", e.getResponseBodyAsString()); // still HTML or error string
} catch (ResourceAccessException e) {
Throwable cause = e.getCause();
if (cause instanceof ConnectException) {
return buildServerDownResponse("503", "Server unavailable - connection refused", e);
} else {
return buildServerDownResponse("503", "Server unreachable: " + e.getMessage(), e);
}
} catch (Exception e) {
log.error("❌ FORM POST Exception: {}", e.getMessage(), e);
@@ -181,7 +222,4 @@ public class HttpService {
return response;
}
}