Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core-api/src/main/java/com/optimizely/ab/Optimizely.java
Original file line number Diff line number Diff line change
Expand Up @@ -1494,14 +1494,15 @@ private Map<String, OptimizelyDecision> decideForKeysInternal(@Nonnull Optimizel
for (int i = 0; i < flagsWithoutForcedDecision.size(); i++) {
DecisionResponse<FeatureDecision> decision = decisionList.get(i);
boolean error = decision.isError();
List<String> reasons = decision.getReasons().toReport();
String experimentKey = null;
if (decision.getResult() != null && decision.getResult().experiment != null) {
experimentKey = decision.getResult().experiment.getKey();
}
String flagKey = flagsWithoutForcedDecision.get(i).getKey();

if (error) {
OptimizelyDecision optimizelyDecision = OptimizelyDecision.newErrorDecision(flagKey, user, DecisionMessage.CMAB_ERROR.reason(experimentKey));
OptimizelyDecision optimizelyDecision = OptimizelyDecision.newErrorDecision(flagKey, user, reasons);
decisionMap.put(flagKey, optimizelyDecision);
if (validKeys.contains(flagKey)) {
validKeys.remove(flagKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,11 +957,12 @@ private DecisionResponse<CmabDecision> getDecisionForCmabExperiment(@Nonnull Pro
// User is in CMAB allocation, proceed to CMAB decision
try {
CmabDecision cmabDecision = cmabService.getDecision(projectConfig, userContext, experiment.getId(), options);

String message = String.format("Successfully fetched CMAB decision %s for experiment %s.", cmabDecision.toString(), experiment.getKey());
reasons.addInfo(message);
return new DecisionResponse<>(cmabDecision, reasons);
} catch (Exception e) {
String errorMessage = String.format("CMAB fetch failed for experiment \"%s\"", experiment.getKey());
reasons.addInfo(errorMessage);
String errorMessage = String.format("Failed to fetch CMAB data for experiment %s.", experiment.getKey());
reasons.addError(errorMessage);
logger.error("{} {}", errorMessage, e.getMessage());

return new DecisionResponse<>(null, reasons, true, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,20 @@ public static OptimizelyDecision newErrorDecision(@Nonnull String key,
user,
Arrays.asList(error));
}


public static OptimizelyDecision newErrorDecision(@Nonnull String key,
@Nonnull OptimizelyUserContext user,
@Nonnull List<String> reasons) {
return new OptimizelyDecision(
null,
false,
new OptimizelyJSON(Collections.emptyMap()),
null,
key,
user,
reasons);
}

@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) return false;
Expand Down
3 changes: 1 addition & 2 deletions core-api/src/test/java/com/optimizely/ab/OptimizelyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5090,7 +5090,6 @@ public void identifyUser() {
@Test
public void testDecideReturnsErrorDecisionWhenDecisionServiceFails() throws Exception {
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));

// Use the CMAB datafile
Optimizely optimizely = Optimizely.builder()
.withDatafile(validConfigJsonCMAB())
Expand All @@ -5099,6 +5098,7 @@ public void testDecideReturnsErrorDecisionWhenDecisionServiceFails() throws Exce

// Mock decision service to return an error from CMAB
DecisionReasons reasons = new DefaultDecisionReasons();
reasons.addError("Failed to fetch CMAB data for experiment exp-cmab.");
FeatureDecision errorFeatureDecision = new FeatureDecision(new Experiment("123", "exp-cmab", "123"), null, FeatureDecision.DecisionSource.ROLLOUT);
DecisionResponse<FeatureDecision> errorDecisionResponse = new DecisionResponse<>(
errorFeatureDecision,
Expand Down Expand Up @@ -5129,7 +5129,6 @@ public void testDecideReturnsErrorDecisionWhenDecisionServiceFails() throws Exce
OptimizelyUserContext userContext = optimizely.createUserContext("test_user");
OptimizelyDecision decision = userContext.decide("feature_1"); // This is the feature flag key from cmab-config.json

System.out.println("reasons: " + decision.getReasons());
// Verify the decision contains the error information
assertFalse(decision.getEnabled());
assertNull(decision.getVariationKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private String doFetch(String url, String requestBody) {
request.setHeader("content-type", "application/json");
CloseableHttpResponse response = null;
try {
logger.info("Fetching CMAB decision: {} with body: {}", url, requestBody);
response = httpClient.execute(request);

if (!CmabClientHelper.isSuccessStatusCode(response.getStatusLine().getStatusCode())) {
Expand Down
Loading