Skip to content

Commit

Permalink
Allow for returning of error messages (GeyserMC#955)
Browse files Browse the repository at this point in the history
* Allow for returning of error messages

* Fix request not sending before error check
  • Loading branch information
rtm516 authored Jul 14, 2020
1 parent af0182d commit 64d5390
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions connector/src/main/java/org/geysermc/connector/utils/WebUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,7 @@ public static String getBody(String reqURL) {
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Geyser-" + GeyserConnector.getInstance().getPlatformType().toString() + "/" + GeyserConnector.VERSION); // Otherwise Java 8 fails on checking updates

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
content.append("\n");
}

in.close();
con.disconnect();

return content.toString();
return connectionToString(con);
} catch (Exception e) {
return e.getMessage();
}
Expand Down Expand Up @@ -99,7 +87,27 @@ public static String post(String reqURL, String postContent) throws IOException
out.write(postContent.getBytes(StandardCharsets.UTF_8));
out.close();

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
return connectionToString(con);
}

/**
* Get the string output from the passed {@link HttpURLConnection}
*
* @param con The connection to get the string from
* @return The body of the returned page
* @throws IOException
*/
private static String connectionToString(HttpURLConnection con) throws IOException {
// Send the request (we dont use this but its required for getErrorStream() to work)
int code = con.getResponseCode();

// Read the error message if there is one if not just read normally
InputStream inputStream = con.getErrorStream();
if (inputStream == null) {
inputStream = con.getInputStream();
}

BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
StringBuffer content = new StringBuffer();

Expand Down

0 comments on commit 64d5390

Please sign in to comment.