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) { catch (SheepItException e) {
throw e; throw e;
} }
catch (NoRouteToHostException e) { catch (IOException e) {
if (e.getCause() instanceof NoRouteToHostException || e.getCause() instanceof UnknownHostException) {
throw new SheepItServerDown(); throw new SheepItServerDown();
} }
catch (UnknownHostException e) { StringWriter sw = new StringWriter();
throw new SheepItServerDown(); PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
throw new SheepItException("error requestJob: unknown IO exception " + e + " stacktrace: " + sw.toString());
} }
catch (Exception e) { catch (Exception e) {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
@@ -458,30 +461,7 @@ public class ServerRequest extends Thread {
} }
public Response HTTPRequest(HttpUrl.Builder httpUrlBuilder, RequestBody data_) throws IOException { public Response HTTPRequest(HttpUrl.Builder httpUrlBuilder, RequestBody data_) throws IOException {
String url = httpUrlBuilder.build().toString(); return HTTPRequest(httpUrlBuilder, data_, false);
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());
}
} }
public Response HTTPRequest(HttpUrl.Builder httpUrlBuilder, RequestBody data_, boolean checkIsSuccessful) throws IOException { 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()); throw new ConnectException("Unexpected response from HTTP Stack " + e.getMessage());
} }
catch (IOException e) { catch (IOException e) {
throw new IOException("Unexpected response from HTTP Stack" + e.getMessage()); throw new IOException("Unexpected response from HTTP Stack " + e.getMessage(), e);
} }
} }