Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #74 from peternied/error
Browse files Browse the repository at this point in the history
Handle error responses that are not json in nature
  • Loading branch information
Peter Nied committed Apr 23, 2016
2 parents cdacec9 + 4732f0d commit 685a4c7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ mavenGroupId = com.onedrive.sdk
mavenArtifactId = onedrive-sdk-android
mavenMajorVersion = 1
mavenMinorVersion = 1
mavenPatchVersion = 4
mavenPatchVersion = 5
nightliesUrl = http://dl.bintray.com/onedrive/Maven
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ public String getJsonResponse() {

@Override
public Map<String, String> getHeaders() {
return new HashMap<>();
final HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return headers;
}
};
mProvider.setConnectionFactory(new MockSingleConnectionFactory(new TestDataConnection(data)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
*/
public class DefaultHttpProvider implements IHttpProvider {

/**
* The content type header
*/
static final String ContentTypeHeaderName = "Content-Type";

/**
* The content type for json responses
*/
static final String JsonContentType = "application/json";

/**
* The serializer.
*/
Expand Down Expand Up @@ -171,10 +181,8 @@ private <Result, Body> Result sendRequestInternal(final IHttpRequest request,
final IProgressCallback<Result> progress)
throws ClientException {
final int defaultBufferSize = 4096;
final String contentTypeHeaderName = "Content-Type";
final String contentLengthHeaderName = "Content-Length";
final String binaryContentType = "application/octet-stream";
final String jsonContentType = "application/json";
final int httpClientErrorResponseCode = 400;
final int httpNoBodyResponseCode = 204;
final int httpAcceptedResponseCode = 202;
Expand Down Expand Up @@ -203,13 +211,13 @@ private <Result, Body> Result sendRequestInternal(final IHttpRequest request,
} else if (serializable instanceof byte[]) {
mLogger.logDebug("Sending byte[] as request body");
bytesToWrite = (byte[]) serializable;
connection.addRequestHeader(contentTypeHeaderName, binaryContentType);
connection.addRequestHeader(ContentTypeHeaderName, binaryContentType);
connection.addRequestHeader(contentLengthHeaderName, "" + bytesToWrite.length);
} else {
mLogger.logDebug("Sending " + serializable.getClass().getName() + " as request body");
final String serializeObject = mSerializer.serializeObject(serializable);
bytesToWrite = serializeObject.getBytes();
connection.addRequestHeader(contentTypeHeaderName, jsonContentType);
connection.addRequestHeader(ContentTypeHeaderName, JsonContentType);
connection.addRequestHeader(contentLengthHeaderName, "" + bytesToWrite.length);
}

Expand Down Expand Up @@ -273,8 +281,8 @@ private <Result, Body> Result sendRequestInternal(final IHttpRequest request,

final Map<String, String> headers = connection.getHeaders();

final String contentType = headers.get(contentTypeHeaderName);
if (contentType.contains(jsonContentType)) {
final String contentType = headers.get(ContentTypeHeaderName);
if (contentType.contains(JsonContentType)) {
mLogger.logDebug("Response json");
return handleJsonResponse(in, resultClass);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public String getMessage(final boolean verbose) {
sb.append(jsonObject.toString(INDENT_SPACES)).append(NEW_LINE);
} catch (final JSONException ignored) {
sb.append("[Warning: Unable to parse error message body]").append(NEW_LINE);
sb.append(mError.rawObject.toString()).append(NEW_LINE);
}
} else {
sb.append(TRUNCATION_MARKER).append(NEW_LINE).append(NEW_LINE);
Expand Down Expand Up @@ -296,16 +297,27 @@ public static <T> OneDriveServiceException createFromConnection(final IHttpReque

final String responseMessage = connection.getResponseMessage();
final String rawOutput = DefaultHttpProvider.streamToString(connection.getInputStream());
OneDriveErrorResponse error;
try {
error = serializer.deserializeObject(rawOutput, OneDriveErrorResponse.class);
} catch (final Exception ex) {
OneDriveErrorResponse error = null;
Exception parsingException = null;

final String contentType = headers.get(DefaultHttpProvider.ContentTypeHeaderName);
if (contentType != null && contentType.contains(DefaultHttpProvider.JsonContentType)) {
try {
error = serializer.deserializeObject(rawOutput, OneDriveErrorResponse.class);
} catch (final Exception ex) {
parsingException = ex;
}
}

if (error == null) {
error = new OneDriveErrorResponse();
error.error = new OneDriveError();
error.error.code = "Unable to parse error response message";
error.error.message = "Raw error: " + rawOutput;
error.error.innererror = new OneDriveInnerError();
error.error.innererror.code = ex.getMessage();
if (parsingException != null) {
error.error.innererror = new OneDriveInnerError();
error.error.innererror.code = parsingException.getMessage();
}
}

if (responseCode == INTERNAL_SERVER_ERROR) {
Expand Down

0 comments on commit 685a4c7

Please sign in to comment.