From 160325bc0608129d51f93279955bec96426c6b4a Mon Sep 17 00:00:00 2001 From: Marius Kleidl Date: Wed, 25 Oct 2023 10:26:24 +0200 Subject: [PATCH 1/2] Include HTTP response body in error messages --- build.gradle | 4 +-- .../io/tus/java/client/ProtocolException.java | 25 +++++++++++++++++-- .../java/io/tus/java/client/TusClient.java | 7 +++--- .../java/io/tus/java/client/TusUploader.java | 6 ++--- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 14e6f7b..c886089 100644 --- a/build.gradle +++ b/build.gradle @@ -16,8 +16,8 @@ allprojects { // We compile the library using Java 1.7 compatibility // in order to ensure interoperability with older Android platforms. -sourceCompatibility = 1.8 -targetCompatibility = 1.8 +sourceCompatibility = 1.9 +targetCompatibility = 1.9 // load version number from file def config = new ConfigSlurper().parse(new File("${projectDir}/src/main/resources/tus-java-client-version/version.properties").toURI().toURL()) diff --git a/src/main/java/io/tus/java/client/ProtocolException.java b/src/main/java/io/tus/java/client/ProtocolException.java index 79cd722..1a8d993 100644 --- a/src/main/java/io/tus/java/client/ProtocolException.java +++ b/src/main/java/io/tus/java/client/ProtocolException.java @@ -1,7 +1,9 @@ package io.tus.java.client; import java.io.IOException; +import java.io.InputStream; import java.net.HttpURLConnection; +import java.nio.charset.StandardCharsets; /** * This exception is thrown if the server sends a request with an unexpected status code or @@ -19,7 +21,7 @@ public ProtocolException(String message) { } /** - Instantiates a new Object of type {@link ProtocolException}. + * Instantiates a new Object of type {@link ProtocolException}. * @param message Message to be thrown with the exception. * @param connection {@link HttpURLConnection}, where the error occurred. */ @@ -29,7 +31,26 @@ public ProtocolException(String message, HttpURLConnection connection) { } /** - * Returns the {@link HttpURLConnection} instances, which caused the error. + * Create a new {@link ProtocolException} instance caused by an unexpected status code. + * @param connection {@link HttpURLConnection}, where the error occurred. + * @param action Description of the action when the error occurred. + * @return {@link HttpURLConnection} + */ + static ProtocolException unexpectedStatusCode(HttpURLConnection connection, String action) throws IOException { + int code = connection.getResponseCode(); + String response = "n/a"; + //System.out.println(connection.getInputStream()); + InputStream responseStream = connection.getErrorStream(); + if (responseStream != null) { + response = new String(responseStream.readAllBytes(), StandardCharsets.UTF_8).trim(); + } + + return new ProtocolException( + "unexpected status code (" + code + ") while " + action + "; response is: " + response, connection); + } + + /** + * Returns the {@link HttpURLConnection} instance which caused the error. * @return {@link HttpURLConnection} */ public HttpURLConnection getCausingConnection() { diff --git a/src/main/java/io/tus/java/client/TusClient.java b/src/main/java/io/tus/java/client/TusClient.java index a5cac6a..908ddee 100644 --- a/src/main/java/io/tus/java/client/TusClient.java +++ b/src/main/java/io/tus/java/client/TusClient.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.Map; /** @@ -208,8 +209,7 @@ public TusUploader createUpload(@NotNull TusUpload upload) throws ProtocolExcept int responseCode = connection.getResponseCode(); if (!(responseCode >= 200 && responseCode < 300)) { - throw new ProtocolException( - "unexpected status code (" + responseCode + ") while creating upload", connection); + throw ProtocolException.unexpectedStatusCode(connection, "creating upload"); } String urlStr = connection.getHeaderField("Location"); @@ -303,8 +303,7 @@ public TusUploader beginOrResumeUploadFromURL(@NotNull TusUpload upload, @NotNul int responseCode = connection.getResponseCode(); if (!(responseCode >= 200 && responseCode < 300)) { - throw new ProtocolException( - "unexpected status code (" + responseCode + ") while resuming upload", connection); + throw ProtocolException.unexpectedStatusCode(connection, "resuming upload"); } String offsetStr = connection.getHeaderField("Upload-Offset"); diff --git a/src/main/java/io/tus/java/client/TusUploader.java b/src/main/java/io/tus/java/client/TusUploader.java index d84bd8b..5b9083a 100644 --- a/src/main/java/io/tus/java/client/TusUploader.java +++ b/src/main/java/io/tus/java/client/TusUploader.java @@ -337,11 +337,8 @@ private void finishConnection() throws ProtocolException, IOException { if (connection != null) { int responseCode = connection.getResponseCode(); - connection.disconnect(); - if (!(responseCode >= 200 && responseCode < 300)) { - throw new ProtocolException("unexpected status code (" + responseCode + ") while uploading chunk", - connection); + throw ProtocolException.unexpectedStatusCode(connection, "uploading chunk"); } // TODO detect changes and seek accordingly @@ -358,6 +355,7 @@ private void finishConnection() throws ProtocolException, IOException { connection); } + connection.disconnect(); connection = null; } } From 96f3988acfc3799adaf495a88144531e646e4c4c Mon Sep 17 00:00:00 2001 From: Subramanian1503 <48426449+Subramanian1503@users.noreply.github.com> Date: Thu, 11 Jan 2024 12:43:46 +0530 Subject: [PATCH 2/2] Removed unused package import of StandardCharset (#104) - Since we are not using the package in TusClient.java, we can remove from the file. --- src/main/java/io/tus/java/client/TusClient.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/io/tus/java/client/TusClient.java b/src/main/java/io/tus/java/client/TusClient.java index 908ddee..3e2eb5d 100644 --- a/src/main/java/io/tus/java/client/TusClient.java +++ b/src/main/java/io/tus/java/client/TusClient.java @@ -7,7 +7,6 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.Map; /**