Skip to content

Commit 8c2f7d5

Browse files
committed
refactor(ServiceResponseException): Iterate over error message keys
1 parent cdc7bfd commit 8c2f7d5

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class ServiceResponseException extends RuntimeException {
3434
private static final String MESSAGE_ERROR = "error";
3535
private static final String MESSAGE_ERROR_2 = "error_message";
3636
private static final String MESSAGE_ERROR_3 = "message";
37+
private static final String[] ERROR_KEYS = { MESSAGE_ERROR, MESSAGE_ERROR_2, MESSAGE_ERROR_3 };
3738

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

@@ -58,12 +59,11 @@ public ServiceResponseException(int statusCode, Response response) {
5859
String responseString = ResponseUtils.getString(response);
5960
try {
6061
final JsonObject jsonObject = ResponseUtils.getJsonObject(responseString);
61-
if (jsonObject.has(MESSAGE_ERROR)) {
62-
this.message = jsonObject.remove(MESSAGE_ERROR).getAsString();
63-
} else if (jsonObject.has(MESSAGE_ERROR_2)) {
64-
this.message = jsonObject.remove(MESSAGE_ERROR_2).getAsString();
65-
} else if (jsonObject.has(MESSAGE_ERROR_3)) {
66-
this.message = jsonObject.remove(MESSAGE_ERROR_3).getAsString();
62+
for (String errorKey : ERROR_KEYS) {
63+
if (jsonObject.has(errorKey)) {
64+
this.message = jsonObject.remove(errorKey).getAsString();
65+
break;
66+
}
6767
}
6868
this.debuggingInfo = GsonSingleton.getGson().fromJson(jsonObject, debuggingInfoType);
6969
} catch (final Exception e) {

0 commit comments

Comments
 (0)