Skip to content

Commit 140f67a

Browse files
authored
Merge pull request #176 from redhat-developer/issue-174
Issue 174
2 parents 5e10954 + a2c7228 commit 140f67a

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

source/rhoas/src/main/java/com/openshift/cloud/beans/AccessTokenSecretTool.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.openshift.cloud.beans;
22

3+
import com.openshift.cloud.ApiException;
34
import com.openshift.cloud.controllers.ConditionAwareException;
45
import com.openshift.cloud.controllers.ConditionUtil;
56
import com.openshift.cloud.v1alpha.models.KafkaCondition;
@@ -144,13 +145,16 @@ private String exchangeToken(String offlineToken) throws ConditionAwareException
144145
} else {
145146
LOG.log(
146147
Level.SEVERE, String.format("Exchange token failed with error %s", response.body()));
148+
// Reusing API error but only with status code
149+
var apiError =
150+
ConditionUtil.getStandarizedErrorMessage(new ApiException(response.statusCode(), null));
147151
throw new ConditionAwareException(
148152
response.body(),
149153
null,
150154
KafkaCondition.Type.AcccesTokenSecretValid,
151155
KafkaCondition.Status.False,
152156
String.format("Http Error Code %d", response.statusCode()),
153-
ConditionUtil.getStandarizedErrorMessage(response.statusCode()));
157+
apiError);
154158
}
155159
} catch (IOException | InterruptedException e) {
156160
throw new ConditionAwareException(

source/rhoas/src/main/java/com/openshift/cloud/controllers/ConditionUtil.java

+21-36
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static com.openshift.cloud.v1alpha.models.KafkaCondition.Status.False;
44
import static com.openshift.cloud.v1alpha.models.KafkaCondition.Status.True;
55

6+
import com.fasterxml.jackson.core.type.TypeReference;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
68
import com.openshift.cloud.ApiException;
79
import com.openshift.cloud.utils.InvalidUserInputException;
810
import com.openshift.cloud.v1alpha.models.*;
@@ -11,8 +13,8 @@
1113
import java.time.ZonedDateTime;
1214
import java.time.format.DateTimeFormatter;
1315
import java.util.ArrayList;
16+
import java.util.HashMap;
1417
import java.util.List;
15-
import java.util.concurrent.atomic.AtomicBoolean;
1618
import java.util.logging.Level;
1719
import java.util.logging.Logger;
1820
import java.util.stream.Collectors;
@@ -213,45 +215,28 @@ private static List<KafkaCondition> kafkaConnectionDefaultConditions(long genera
213215
.setStatus(Status.Unknown));
214216
}
215217

216-
public static boolean allTrue(List<KafkaCondition> conditions) {
217-
AtomicBoolean allTrue = new AtomicBoolean(true);
218-
for (var cond : conditions) {
219-
if (cond.getStatus() != True) {
220-
allTrue.set(false);
221-
break;
222-
}
223-
}
224-
;
225-
return allTrue.get();
226-
}
227-
228218
/**
229-
* Given an api exception, map the http error code to a known string.
219+
* Map the api exception to proper error
230220
*
231-
* @param e an exception thrown by a call to the MAS API
221+
* @param e exception using APIException object
232222
* @return a human readable String to be set as the message property of a failed condition
233223
*/
234224
public static String getStandarizedErrorMessage(ApiException e) {
225+
var statusCode = e.getCode();
226+
var reason = "";
227+
var errorObject = new HashMap<String, Object>();
235228

236-
switch (e.getCode()) {
237-
case 504: // SC_GATEWAY_TIMEOUT:
238-
case 500: // SC_INTERNAL_SERVER_ERROR:
239-
case 401: // HttpStatus.SC_UNAUTHORIZED:
240-
case 403: // HttpStatus.SC_FORBIDDEN:
241-
return getStandarizedErrorMessage(e.getCode());
242-
case 400: // HttpStatus.SC_BAD_REQUEST:
243-
return "Invalid request " + e.getMessage();
244-
default:
245-
return e.getMessage();
229+
try {
230+
if (e.getMessage() != null) {
231+
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<>() {};
232+
errorObject = new ObjectMapper().readValue(e.getMessage(), typeRef);
233+
reason = errorObject.getOrDefault("reason", "").toString();
234+
}
235+
} catch (Exception exception) {
236+
LOG.warning(
237+
String.format("Cannot process error returned by api: %s", exception.getMessage()));
246238
}
247-
}
248-
/**
249-
* Map the http error code to a known string.
250-
*
251-
* @param statusCode a non 200 HTTP code returned by the system.
252-
* @return a human readable String to be set as the message property of a failed condition
253-
*/
254-
public static String getStandarizedErrorMessage(int statusCode) {
239+
255240
switch (statusCode) {
256241
case 504: // SC_GATEWAY_TIMEOUT:
257242
return "Server timeout. Server is not responding";
@@ -260,13 +245,13 @@ public static String getStandarizedErrorMessage(int statusCode) {
260245
case 500: // SC_INTERNAL_SERVER_ERROR:
261246
return "Unknown server error";
262247
case 400: // HttpStatus.SC_BAD_REQUEST:
263-
return "Provided user input is invalid";
248+
return String.format("Provided user input is invalid: %s", reason);
264249
case 401: // HttpStatus.SC_UNAUTHORIZED:
265250
return "Cannot authenticate user with the service";
266251
case 403: // HttpStatus.SC_FORBIDDEN:
267-
return "User not authorized to access the service";
252+
return String.format("User not authorized to access the service: %s", reason);
268253
default:
269-
return String.format("Http Error Code %d", statusCode);
254+
return String.format("Error %d: %s", statusCode, reason);
270255
}
271256
}
272257

0 commit comments

Comments
 (0)