Skip to content

Commit 6307b0c

Browse files
committed
refactor: Try and pull error message from IAM response
1 parent d0ad737 commit 6307b0c

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class ServiceResponseException extends RuntimeException {
3434
private static final String ERRORS_KEY = "errors";
3535
private static final String MESSAGE_STRING = "message";
3636
private static final String ERROR_STRING = "error";
37+
private static final String ERROR_MESSAGE = "errorMessage";
3738

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

@@ -65,6 +66,8 @@ public ServiceResponseException(int statusCode, Response response) {
6566
this.message = jsonObject.remove(ERROR_STRING).getAsString();
6667
} else if (jsonObject.has(MESSAGE_STRING)) {
6768
this.message = jsonObject.remove(MESSAGE_STRING).getAsString();
69+
} else if (jsonObject.has(ERROR_MESSAGE)) {
70+
this.message = jsonObject.remove(ERROR_MESSAGE).getAsString();
6871
}
6972
this.debuggingInfo = GsonSingleton.getGson().fromJson(jsonObject, debuggingInfoType);
7073
} catch (final Exception e) {

src/main/java/com/ibm/cloud/sdk/core/service/security/IamTokenManager.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.ibm.cloud.sdk.core.http.HttpMediaType;
1818
import com.ibm.cloud.sdk.core.http.RequestBuilder;
1919
import com.ibm.cloud.sdk.core.http.ResponseConverter;
20+
import com.ibm.cloud.sdk.core.service.exception.ServiceResponseException;
2021
import com.ibm.cloud.sdk.core.util.CredentialUtils;
2122
import com.ibm.cloud.sdk.core.util.ResponseConverterUtils;
2223
import okhttp3.Call;
@@ -163,7 +164,7 @@ private boolean isAccessTokenExpired() {
163164
* if it has been at least 7 days and 1 hour since the last token was
164165
* retrieved.
165166
*
166-
* @returns whether the current managed refresh token is expired or not
167+
* @return whether the current managed refresh token is expired or not
167168
*/
168169
private boolean isRefreshTokenExpired() {
169170
if (tokenData.getExpiration() == null) {
@@ -193,6 +194,12 @@ public void run() {
193194

194195
try {
195196
okhttp3.Response response = call.execute();
197+
198+
// handle possible errors
199+
if (response.code() >= 400) {
200+
throw new ServiceResponseException(response.code(), response);
201+
}
202+
196203
returnToken[0] = converter.convert(response);
197204
} catch (IOException e) {
198205
LOG.severe(ERROR_MESSAGE);

0 commit comments

Comments
 (0)