Skip to content

Commit 50e26f8

Browse files
committed
refactor: Change error parsing logic to match other SDKs
1 parent 727d028 commit 50e26f8

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
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)