Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include HTTP response body in error messages #99

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/io/tus/java/client/ProtocolException.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
*/
Expand All @@ -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() {
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/tus/java/client/TusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,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");
Expand Down Expand Up @@ -303,8 +302,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");
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/tus/java/client/TusUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -358,6 +355,7 @@ private void finishConnection() throws ProtocolException, IOException {
connection);
}

connection.disconnect();
connection = null;
}
}
Expand Down