Skip to content

Commit 6577ca1

Browse files
authored
Merge pull request #21 from IBM/error-parsing
Error parsing
2 parents 727d028 + b62a155 commit 6577ca1

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<compiler-plugin-version>3.8.0</compiler-plugin-version>
4545
<okhttp3-version>3.12.1</okhttp3-version>
4646
<logback-version>1.2.3</logback-version>
47-
<guava-version>27.0.1-jre</guava-version>
47+
<guava-version>27.1-android</guava-version>
4848
<gson-version>2.8.5</gson-version>
4949
<commons-io-version>2.6</commons-io-version>
5050
<commons-lang3-version>3.8.1</commons-lang3-version>

src/main/java/com/ibm/cloud/sdk/core/service/exception/ServiceResponseException.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ public class ServiceResponseException extends RuntimeException {
3131
private static final long serialVersionUID = 1L;
3232

3333
/** Potential error message keys. */
34-
private static final String MESSAGE_ERROR = "error";
35-
private static final String MESSAGE_ERROR_2 = "error_message";
36-
private static final String MESSAGE_ERROR_3 = "message";
37-
private static final String[] ERROR_KEYS = { MESSAGE_ERROR, MESSAGE_ERROR_2, MESSAGE_ERROR_3 };
34+
private static final String ERRORS_KEY = "errors";
35+
private static final String MESSAGE_STRING = "message";
36+
private static final String ERROR_STRING = "error";
3837

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

@@ -59,11 +58,13 @@ public ServiceResponseException(int statusCode, Response response) {
5958
String responseString = ResponseUtils.getString(response);
6059
try {
6160
final JsonObject jsonObject = ResponseUtils.getJsonObject(responseString);
62-
for (String errorKey : ERROR_KEYS) {
63-
if (jsonObject.has(errorKey)) {
64-
this.message = jsonObject.remove(errorKey).getAsString();
65-
break;
66-
}
61+
if (jsonObject.has(ERRORS_KEY)) {
62+
this.message = jsonObject.remove(ERRORS_KEY).getAsJsonArray().get(0).getAsJsonObject().remove(MESSAGE_STRING)
63+
.getAsString();
64+
} else if (jsonObject.has(ERROR_STRING)) {
65+
this.message = jsonObject.remove(ERROR_STRING).getAsString();
66+
} else if (jsonObject.has(MESSAGE_STRING)) {
67+
this.message = jsonObject.remove(MESSAGE_STRING).getAsString();
6768
}
6869
this.debuggingInfo = GsonSingleton.getGson().fromJson(jsonObject, debuggingInfoType);
6970
} catch (final Exception e) {

0 commit comments

Comments
 (0)