Skip to content

Commit 8eb694e

Browse files
update: improve error logging in DefaultCmabClient for fetchDecision method
1 parent 044c230 commit 8eb694e

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

core-httpclient-impl/src/main/java/com/optimizely/ab/cmab/DefaultCmabClient.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class DefaultCmabClient implements CmabClient {
2424

2525
private static final Logger logger = LoggerFactory.getLogger(DefaultCmabClient.class);
2626
// Update constants to match JS error messages format
27-
private static final String CMAB_FETCH_FAILED = "CMAB decision fetch failed with status: %d";
27+
private static final String CMAB_FETCH_FAILED = "CMAB decision fetch failed with status: %s";
2828
private static final String INVALID_CMAB_FETCH_RESPONSE = "Invalid CMAB fetch response";
2929
private static final Pattern VARIATION_ID_PATTERN = Pattern.compile("\"variation_id\"\\s*:\\s*\"?([^\"\\s,}]+)\"?");
3030
private static final String CMAB_PREDICTION_ENDPOINT = "https://prediction.cmab.optimizely.com/predict/%s";
@@ -83,8 +83,9 @@ private CompletableFuture<String> doFetch(String url, String requestBody, String
8383
try {
8484
request.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));
8585
} catch (Exception e) {
86-
logger.error(String.format(CMAB_FETCH_FAILED, e));
87-
throw new CompletionException(new RuntimeException(String.format(CMAB_FETCH_FAILED, e)));
86+
String errorMsg = String.format(CMAB_FETCH_FAILED, "encoding error");
87+
logger.error(errorMsg + " for user: {}", userId, e);
88+
throw new CompletionException(new RuntimeException(errorMsg, e));
8889
}
8990

9091
CloseableHttpResponse response = null;
@@ -94,18 +95,20 @@ private CompletableFuture<String> doFetch(String url, String requestBody, String
9495

9596
int statusCode = response.getStatusLine().getStatusCode();
9697

98+
// Status code error
9799
if (!isSuccessStatusCode(statusCode)) {
98-
logger.error("CMAB fetch failed (Response code: {}) for user: {}", statusCode, userId);
99-
// Status code error (like JS: new OptimizelyError(CMAB_FETCH_FAILED, response.statusCode))
100-
throw new CompletionException(new RuntimeException(String.format(CMAB_FETCH_FAILED, statusCode)));
100+
String errorMsg = String.format(CMAB_FETCH_FAILED, statusCode);
101+
logger.error(errorMsg + " for user: {}", userId);
102+
throw new CompletionException(new RuntimeException(errorMsg));
101103
}
102104

103105
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
104106
logger.debug("CMAB response received for user: {}", userId);
105107

106108

109+
// Invalid response error
107110
if (!validateResponse(responseBody)) {
108-
logger.error(INVALID_CMAB_FETCH_RESPONSE);
111+
logger.error(INVALID_CMAB_FETCH_RESPONSE + " for user: {}", userId);
109112
throw new CompletionException(new RuntimeException(INVALID_CMAB_FETCH_RESPONSE));
110113
}
111114

@@ -115,8 +118,10 @@ private CompletableFuture<String> doFetch(String url, String requestBody, String
115118
return variationId;
116119

117120
} catch (IOException e) {
118-
logger.error(String.format(CMAB_FETCH_FAILED, e));
119-
throw new CompletionException(new RuntimeException(String.format(CMAB_FETCH_FAILED, e)));
121+
// IO error
122+
String errorMsg = String.format(CMAB_FETCH_FAILED, "network error");
123+
logger.error(errorMsg + " for user: {}", userId, e);
124+
throw new CompletionException(new RuntimeException(errorMsg, e));
120125
} finally {
121126
closeHttpResponse(response);
122127
}

0 commit comments

Comments
 (0)