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

Commit 68fc6d7

Browse files
author
Peter Nied
committed
Handle error responses that are not json in nature
Some error messages that can comeback from the service can be in non-json formats. Malformed URLs are an example of an error that can return this kind of message. Instead of assuming all messages are json, instead check the content-type header and write the raw error message info on toString for the exception details. Fixes #53
1 parent 4f9392e commit 68fc6d7

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

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: 17 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,26 @@ 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+
if (headers.get(DefaultHttpProvider.ContentTypeHeaderName).contains(DefaultHttpProvider.JsonContentType)) {
304+
try {
305+
error = serializer.deserializeObject(rawOutput, OneDriveErrorResponse.class);
306+
} catch (final Exception ex) {
307+
parsingException = ex;
308+
}
309+
}
310+
311+
if (error == null) {
303312
error = new OneDriveErrorResponse();
304313
error.error = new OneDriveError();
305314
error.error.code = "Unable to parse error response message";
306315
error.error.message = "Raw error: " + rawOutput;
307-
error.error.innererror = new OneDriveInnerError();
308-
error.error.innererror.code = ex.getMessage();
316+
if (parsingException != null) {
317+
error.error.innererror = new OneDriveInnerError();
318+
error.error.innererror.code = parsingException.getMessage();
319+
}
309320
}
310321

311322
if (responseCode == INTERNAL_SERVER_ERROR) {

0 commit comments

Comments
 (0)