3
3
import static com .openshift .cloud .v1alpha .models .KafkaCondition .Status .False ;
4
4
import static com .openshift .cloud .v1alpha .models .KafkaCondition .Status .True ;
5
5
6
+ import com .fasterxml .jackson .core .type .TypeReference ;
7
+ import com .fasterxml .jackson .databind .ObjectMapper ;
6
8
import com .openshift .cloud .ApiException ;
7
9
import com .openshift .cloud .utils .InvalidUserInputException ;
8
10
import com .openshift .cloud .v1alpha .models .*;
11
13
import java .time .ZonedDateTime ;
12
14
import java .time .format .DateTimeFormatter ;
13
15
import java .util .ArrayList ;
16
+ import java .util .HashMap ;
14
17
import java .util .List ;
15
- import java .util .concurrent .atomic .AtomicBoolean ;
16
18
import java .util .logging .Level ;
17
19
import java .util .logging .Logger ;
18
20
import java .util .stream .Collectors ;
@@ -213,45 +215,28 @@ private static List<KafkaCondition> kafkaConnectionDefaultConditions(long genera
213
215
.setStatus (Status .Unknown ));
214
216
}
215
217
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
-
228
218
/**
229
- * Given an api exception, map the http error code to a known string.
219
+ * Map the api exception to proper error
230
220
*
231
- * @param e an exception thrown by a call to the MAS API
221
+ * @param e exception using APIException object
232
222
* @return a human readable String to be set as the message property of a failed condition
233
223
*/
234
224
public static String getStandarizedErrorMessage (ApiException e ) {
225
+ var statusCode = e .getCode ();
226
+ var reason = "" ;
227
+ var errorObject = new HashMap <String , Object >();
235
228
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 ()));
246
238
}
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
+
255
240
switch (statusCode ) {
256
241
case 504 : // SC_GATEWAY_TIMEOUT:
257
242
return "Server timeout. Server is not responding" ;
@@ -260,13 +245,13 @@ public static String getStandarizedErrorMessage(int statusCode) {
260
245
case 500 : // SC_INTERNAL_SERVER_ERROR:
261
246
return "Unknown server error" ;
262
247
case 400 : // HttpStatus.SC_BAD_REQUEST:
263
- return "Provided user input is invalid" ;
248
+ return String . format ( "Provided user input is invalid: %s" , reason ) ;
264
249
case 401 : // HttpStatus.SC_UNAUTHORIZED:
265
250
return "Cannot authenticate user with the service" ;
266
251
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 ) ;
268
253
default :
269
- return String .format ("Http Error Code %d " , statusCode );
254
+ return String .format ("Error %d: %s " , statusCode , reason );
270
255
}
271
256
}
272
257
0 commit comments