From 1dfacaee02f21f65537f480cf23d308a65c2d8ea Mon Sep 17 00:00:00 2001 From: M*C*O Date: Mon, 28 Jul 2025 17:46:23 +0000 Subject: [PATCH] Fix: detection of network exceptions, merge duplicate HTTPRequest functions --- .../sheepit/client/network/ServerRequest.java | 42 +++++-------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/sheepit/client/network/ServerRequest.java b/src/main/java/com/sheepit/client/network/ServerRequest.java index faa8e65..61213df 100644 --- a/src/main/java/com/sheepit/client/network/ServerRequest.java +++ b/src/main/java/com/sheepit/client/network/ServerRequest.java @@ -434,11 +434,14 @@ public class ServerRequest extends Thread { catch (SheepItException e) { throw e; } - catch (NoRouteToHostException e) { - throw new SheepItServerDown(); - } - catch (UnknownHostException e) { - throw new SheepItServerDown(); + catch (IOException e) { + if (e.getCause() instanceof NoRouteToHostException || e.getCause() instanceof UnknownHostException) { + 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 { @@ -507,10 +487,10 @@ public class ServerRequest extends Thread { return response; } catch (ConnectException e) { - throw new ConnectException("Unexpected response from HTTP Stack" + e.getMessage()); + 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); } }