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

Commit 685a4c7

Browse files
author
Peter Nied
committed
Merge pull request #74 from peternied/error
Handle error responses that are not json in nature
2 parents cdacec9 + 4732f0d commit 685a4c7

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ mavenGroupId = com.onedrive.sdk
2323
mavenArtifactId = onedrive-sdk-android
2424
mavenMajorVersion = 1
2525
mavenMinorVersion = 1
26-
mavenPatchVersion = 4
26+
mavenPatchVersion = 5
2727
nightliesUrl = http://dl.bintray.com/onedrive/Maven

onedrivesdk/src/androidTest/java/com/onedrive/sdk/http/DefaultHttpProviderTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ public String getJsonResponse() {
306306

307307
@Override
308308
public Map<String, String> getHeaders() {
309-
return new HashMap<>();
309+
final HashMap<String, String> headers = new HashMap<>();
310+
headers.put("Content-Type", "application/json");
311+
return headers;
310312
}
311313
};
312314
mProvider.setConnectionFactory(new MockSingleConnectionFactory(new TestDataConnection(data)));

onedrivesdk/src/main/java/com/onedrive/sdk/http/DefaultHttpProvider.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@
4747
*/
4848
public class DefaultHttpProvider implements IHttpProvider {
4949

50+
/**
51+
* The content type header
52+
*/
53+
static final String ContentTypeHeaderName = "Content-Type";
54+
55+
/**
56+
* The content type for json responses
57+
*/
58+
static final String JsonContentType = "application/json";
59+
5060
/**
5161
* The serializer.
5262
*/
@@ -171,10 +181,8 @@ private <Result, Body> Result sendRequestInternal(final IHttpRequest request,
171181
final IProgressCallback<Result> progress)
172182
throws ClientException {
173183
final int defaultBufferSize = 4096;
174-
final String contentTypeHeaderName = "Content-Type";
175184
final String contentLengthHeaderName = "Content-Length";
176185
final String binaryContentType = "application/octet-stream";
177-
final String jsonContentType = "application/json";
178186
final int httpClientErrorResponseCode = 400;
179187
final int httpNoBodyResponseCode = 204;
180188
final int httpAcceptedResponseCode = 202;
@@ -203,13 +211,13 @@ private <Result, Body> Result sendRequestInternal(final IHttpRequest request,
203211
} else if (serializable instanceof byte[]) {
204212
mLogger.logDebug("Sending byte[] as request body");
205213
bytesToWrite = (byte[]) serializable;
206-
connection.addRequestHeader(contentTypeHeaderName, binaryContentType);
214+
connection.addRequestHeader(ContentTypeHeaderName, binaryContentType);
207215
connection.addRequestHeader(contentLengthHeaderName, "" + bytesToWrite.length);
208216
} else {
209217
mLogger.logDebug("Sending " + serializable.getClass().getName() + " as request body");
210218
final String serializeObject = mSerializer.serializeObject(serializable);
211219
bytesToWrite = serializeObject.getBytes();
212-
connection.addRequestHeader(contentTypeHeaderName, jsonContentType);
220+
connection.addRequestHeader(ContentTypeHeaderName, JsonContentType);
213221
connection.addRequestHeader(contentLengthHeaderName, "" + bytesToWrite.length);
214222
}
215223

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

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

276-
final String contentType = headers.get(contentTypeHeaderName);
277-
if (contentType.contains(jsonContentType)) {
284+
final String contentType = headers.get(ContentTypeHeaderName);
285+
if (contentType.contains(JsonContentType)) {
278286
mLogger.logDebug("Response json");
279287
return handleJsonResponse(in, resultClass);
280288
} else {

onedrivesdk/src/main/java/com/onedrive/sdk/http/OneDriveServiceException.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public String getMessage(final boolean verbose) {
216216
sb.append(jsonObject.toString(INDENT_SPACES)).append(NEW_LINE);
217217
} catch (final JSONException ignored) {
218218
sb.append("[Warning: Unable to parse error message body]").append(NEW_LINE);
219+
sb.append(mError.rawObject.toString()).append(NEW_LINE);
219220
}
220221
} else {
221222
sb.append(TRUNCATION_MARKER).append(NEW_LINE).append(NEW_LINE);
@@ -296,16 +297,27 @@ public static <T> OneDriveServiceException createFromConnection(final IHttpReque
296297

297298
final String responseMessage = connection.getResponseMessage();
298299
final String rawOutput = DefaultHttpProvider.streamToString(connection.getInputStream());
299-
OneDriveErrorResponse error;
300-
try {
301-
error = serializer.deserializeObject(rawOutput, OneDriveErrorResponse.class);
302-
} catch (final Exception ex) {
300+
OneDriveErrorResponse error = null;
301+
Exception parsingException = null;
302+
303+
final String contentType = headers.get(DefaultHttpProvider.ContentTypeHeaderName);
304+
if (contentType != null && contentType.contains(DefaultHttpProvider.JsonContentType)) {
305+
try {
306+
error = serializer.deserializeObject(rawOutput, OneDriveErrorResponse.class);
307+
} catch (final Exception ex) {
308+
parsingException = ex;
309+
}
310+
}
311+
312+
if (error == null) {
303313
error = new OneDriveErrorResponse();
304314
error.error = new OneDriveError();
305315
error.error.code = "Unable to parse error response message";
306316
error.error.message = "Raw error: " + rawOutput;
307-
error.error.innererror = new OneDriveInnerError();
308-
error.error.innererror.code = ex.getMessage();
317+
if (parsingException != null) {
318+
error.error.innererror = new OneDriveInnerError();
319+
error.error.innererror.code = parsingException.getMessage();
320+
}
309321
}
310322

311323
if (responseCode == INTERNAL_SERVER_ERROR) {

0 commit comments

Comments
 (0)