Skip to content

Commit

Permalink
Merge pull request #21 from IBM/error-parsing
Browse files Browse the repository at this point in the history
Error parsing
  • Loading branch information
lpatino10 authored Mar 26, 2019
2 parents 727d028 + b62a155 commit 6577ca1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<compiler-plugin-version>3.8.0</compiler-plugin-version>
<okhttp3-version>3.12.1</okhttp3-version>
<logback-version>1.2.3</logback-version>
<guava-version>27.0.1-jre</guava-version>
<guava-version>27.1-android</guava-version>
<gson-version>2.8.5</gson-version>
<commons-io-version>2.6</commons-io-version>
<commons-lang3-version>3.8.1</commons-lang3-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ public class ServiceResponseException extends RuntimeException {
private static final long serialVersionUID = 1L;

/** Potential error message keys. */
private static final String MESSAGE_ERROR = "error";
private static final String MESSAGE_ERROR_2 = "error_message";
private static final String MESSAGE_ERROR_3 = "message";
private static final String[] ERROR_KEYS = { MESSAGE_ERROR, MESSAGE_ERROR_2, MESSAGE_ERROR_3 };
private static final String ERRORS_KEY = "errors";
private static final String MESSAGE_STRING = "message";
private static final String ERROR_STRING = "error";

private static final Type debuggingInfoType = new TypeToken<Map<String, Object>>() { }.getType();

Expand All @@ -59,11 +58,13 @@ public ServiceResponseException(int statusCode, Response response) {
String responseString = ResponseUtils.getString(response);
try {
final JsonObject jsonObject = ResponseUtils.getJsonObject(responseString);
for (String errorKey : ERROR_KEYS) {
if (jsonObject.has(errorKey)) {
this.message = jsonObject.remove(errorKey).getAsString();
break;
}
if (jsonObject.has(ERRORS_KEY)) {
this.message = jsonObject.remove(ERRORS_KEY).getAsJsonArray().get(0).getAsJsonObject().remove(MESSAGE_STRING)
.getAsString();
} else if (jsonObject.has(ERROR_STRING)) {
this.message = jsonObject.remove(ERROR_STRING).getAsString();
} else if (jsonObject.has(MESSAGE_STRING)) {
this.message = jsonObject.remove(MESSAGE_STRING).getAsString();
}
this.debuggingInfo = GsonSingleton.getGson().fromJson(jsonObject, debuggingInfoType);
} catch (final Exception e) {
Expand Down

0 comments on commit 6577ca1

Please sign in to comment.