Fix: detection of network exceptions, merge duplicate HTTPRequest functions

This commit is contained in:
M*C*O
2025-07-28 17:46:23 +00:00
committed by Laurent Clouet
parent e23a6d565c
commit 1dfacaee02

View File

@@ -434,11 +434,14 @@ public class ServerRequest extends Thread {
catch (SheepItException e) {
throw e;
}
catch (NoRouteToHostException e) {
catch (IOException e) {
if (e.getCause() instanceof NoRouteToHostException || e.getCause() instanceof UnknownHostException) {
throw new SheepItServerDown();
}
catch (UnknownHostException e) {
throw new SheepItServerDown();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
throw new SheepItException("error requestJob: unknown IO exception " + e + " stacktrace: " + sw.toString());
}
catch (Exception e) {
StringWriter sw = new StringWriter();
@@ -458,30 +461,7 @@ public class ServerRequest extends Thread {
}
public Response HTTPRequest(HttpUrl.Builder httpUrlBuilder, RequestBody data_) throws IOException {
String url = httpUrlBuilder.build().toString();
Request.Builder builder = new Request.Builder().addHeader("User-Agent", HTTP_USER_AGENT).url(url);
this.log.debug("Server::HTTPRequest url(" + url + ")");
if (data_ != null) {
builder.post(data_);
}
Request request = builder.build();
try {
Response response = httpClient.newCall(request).execute();
if (response.isSuccessful() == false) {
this.log.error("Received unsuccessful HTTP response " + response);
}
this.lastRequestTime = new Date().getTime();
return response;
}
catch (IOException e) {
throw new IOException("Unexpected response from HTTP Stack" + e.getMessage());
}
return HTTPRequest(httpUrlBuilder, data_, false);
}
public Response HTTPRequest(HttpUrl.Builder httpUrlBuilder, RequestBody data_, boolean checkIsSuccessful) throws IOException {
@@ -510,7 +490,7 @@ public class ServerRequest extends Thread {
throw new ConnectException("Unexpected response from HTTP Stack " + e.getMessage());
}
catch (IOException e) {
throw new IOException("Unexpected response from HTTP Stack" + e.getMessage());
throw new IOException("Unexpected response from HTTP Stack " + e.getMessage(), e);
}
}