fix: Do not throw an exception if no error stream is present

This commit is contained in:
LisoUseInAIKyrios 2024-04-08 23:43:29 +04:00
parent 116ac59557
commit 1ce4b56bc6
2 changed files with 41 additions and 35 deletions

View File

@ -32,28 +32,6 @@ public class Requester {
return connection;
}
/**
* Parse the {@link HttpURLConnection} response as a String.
* This does not close the url connection. If further requests to this host are unlikely
* in the future, then instead use {@link #parseStringAndDisconnect(HttpURLConnection)}.
*/
public static String parseString(HttpURLConnection connection) throws IOException {
return parseInputStreamAndClose(connection.getInputStream(), true);
}
/**
* Parse the {@link HttpURLConnection} response as a String and disconnect.
*
* <b>Should only be used if other requests to the server are unlikely in the near future</b>
*
* @see #parseString(HttpURLConnection)
*/
public static String parseStringAndDisconnect(HttpURLConnection connection) throws IOException {
String result = parseString(connection);
connection.disconnect();
return result;
}
/**
* Parse the {@link HttpURLConnection}, and closes the underlying InputStream.
*
@ -73,20 +51,48 @@ public class Requester {
}
/**
* Parse the {@link HttpURLConnection}, and closes the underlying InputStream.
* Parse the {@link HttpURLConnection} response as a String.
* This does not close the url connection. If further requests to this host are unlikely
* in the near future, then instead use {@link #parseStringAndDisconnect(HttpURLConnection)}.
*/
public static String parseErrorString(HttpURLConnection connection) throws IOException {
return parseInputStreamAndClose(connection.getErrorStream(), false);
public static String parseString(HttpURLConnection connection) throws IOException {
return parseInputStreamAndClose(connection.getInputStream(), true);
}
/**
* Parse the {@link HttpURLConnection}, close the underlying InputStream, and disconnect.
* Parse the {@link HttpURLConnection} response as a String, and disconnect.
*
* <b>Should only be used if other requests to the server are unlikely in the near future</b>
* <b>Should only be used if other requests to the server in the near future are unlikely</b>
*
* @see #parseString(HttpURLConnection)
*/
public static String parseStringAndDisconnect(HttpURLConnection connection) throws IOException {
String result = parseString(connection);
connection.disconnect();
return result;
}
/**
* Parse the {@link HttpURLConnection} error stream as a String.
* If the server sent no error response data, this returns an empty string.
*/
public static String parseErrorString(HttpURLConnection connection) throws IOException {
InputStream errorStream = connection.getErrorStream();
if (errorStream == null) {
return "";
}
return parseInputStreamAndClose(errorStream, false);
}
/**
* Parse the {@link HttpURLConnection} error stream as a String, and disconnect.
* If the server sent no error response data, this returns an empty string.
*
* Should only be used if other requests to the server are unlikely in the near future.
*
* @see #parseErrorString(HttpURLConnection)
*/
public static String parseErrorJsonAndDisconnect(HttpURLConnection connection) throws IOException {
public static String parseErrorStringAndDisconnect(HttpURLConnection connection) throws IOException {
String result = parseErrorString(connection);
connection.disconnect();
return result;
@ -95,7 +101,7 @@ public class Requester {
/**
* Parse the {@link HttpURLConnection} response into a JSONObject.
* This does not close the url connection. If further requests to this host are unlikely
* in the future, then instead use {@link #parseJSONObjectAndDisconnect(HttpURLConnection)}.
* in the near future, then instead use {@link #parseJSONObjectAndDisconnect(HttpURLConnection)}.
*/
public static JSONObject parseJSONObject(HttpURLConnection connection) throws JSONException, IOException {
return new JSONObject(parseString(connection));
@ -104,7 +110,7 @@ public class Requester {
/**
* Parse the {@link HttpURLConnection}, close the underlying InputStream, and disconnect.
*
* <b>Should only be used if other requests to the server are unlikely in the near future</b>
* <b>Should only be used if other requests to the server in the near future are unlikely</b>
*
* @see #parseJSONObject(HttpURLConnection)
*/
@ -117,7 +123,7 @@ public class Requester {
/**
* Parse the {@link HttpURLConnection}, and closes the underlying InputStream.
* This does not close the url connection. If further requests to this host are unlikely
* in the future, then instead use {@link #parseJSONArrayAndDisconnect(HttpURLConnection)}.
* in the near future, then instead use {@link #parseJSONArrayAndDisconnect(HttpURLConnection)}.
*/
public static JSONArray parseJSONArray(HttpURLConnection connection) throws JSONException, IOException {
return new JSONArray(parseString(connection));
@ -126,7 +132,7 @@ public class Requester {
/**
* Parse the {@link HttpURLConnection}, close the underlying InputStream, and disconnect.
*
* <b>Should only be used if other requests to the server are unlikely in the near future</b>
* <b>Should only be used if other requests to the server in the near future are unlikely</b>
*
* @see #parseJSONArray(HttpURLConnection)
*/

View File

@ -159,13 +159,13 @@ public class SBRequester {
messageToToast = str("revanced_sb_submit_failed_duplicate");
break;
case 403:
messageToToast = str("revanced_sb_submit_failed_forbidden", Requester.parseErrorJsonAndDisconnect(connection));
messageToToast = str("revanced_sb_submit_failed_forbidden", Requester.parseErrorStringAndDisconnect(connection));
break;
case 429:
messageToToast = str("revanced_sb_submit_failed_rate_limit");
break;
case 400:
messageToToast = str("revanced_sb_submit_failed_invalid", Requester.parseErrorJsonAndDisconnect(connection));
messageToToast = str("revanced_sb_submit_failed_invalid", Requester.parseErrorStringAndDisconnect(connection));
break;
default:
messageToToast = str("revanced_sb_submit_failed_unknown_error", responseCode, connection.getResponseMessage());
@ -223,7 +223,7 @@ public class SBRequester {
break;
case 403:
Utils.showToastLong(
str("revanced_sb_vote_failed_forbidden", Requester.parseErrorJsonAndDisconnect(connection)));
str("revanced_sb_vote_failed_forbidden", Requester.parseErrorStringAndDisconnect(connection)));
break;
default:
Utils.showToastLong(