Skip to content

Commit a4d9c28

Browse files
committed
fix: Mark the EXCEPTION result of an overridden flag
When the evaluation of a flag throws, the error result now carries the override-affected marking of the flag's own definition, which was read before the failure. The spec marks a failed evaluation when a definition it read came from the override store.
1 parent 2b15eed commit a4d9c28

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/InputValidatingEvaluator.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ EvalResultAndFlag evaluate(String flagKey, LDContext context, LDValue defaultVal
155155
} else {
156156
recorder.recordEvaluationError(featureFlag, context, defaultValue, ErrorKind.EXCEPTION);
157157
}
158-
return new EvalResultAndFlag(EvalResult.of(defaultValue, NO_VARIATION, EvaluationReason.exception(e)), null);
158+
// The flag's definition was read before the failure, so its own marker decides the marking of
159+
// the error result.
160+
boolean overrideAffected = featureFlag != null && featureFlag.isOverride();
161+
return new EvalResultAndFlag(
162+
EvalResult.of(defaultValue, NO_VARIATION, EvaluationReason.exception(e)).withOverrideAffected(overrideAffected),
163+
null);
159164
}
160165
}
161166

‎lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/InputValidatingEvaluatorOverrideTest.java‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.launchdarkly.sdk.server.DataModel.FEATURES;
2020
import static com.launchdarkly.sdk.server.EvaluatorTestUtil.BASE_USER;
2121
import static com.launchdarkly.sdk.server.ModelBuilders.flagBuilder;
22+
import static com.launchdarkly.sdk.server.ModelBuilders.prerequisite;
2223
import static com.launchdarkly.sdk.server.TestComponents.nullLogger;
2324
import static org.junit.Assert.assertEquals;
2425
import static org.junit.Assert.assertFalse;
@@ -49,6 +50,59 @@ public boolean isInitialized() {
4950
};
5051
}
5152

53+
// A store that holds the given flags and fails when the given key is read, as a store does
54+
// when its database is unreachable.
55+
private static ReadOnlyStore storeFailingOn(String failingKey, FeatureFlag... flags) {
56+
ReadOnlyStore base = storeWith(flags);
57+
return new ReadOnlyStore() {
58+
@Override
59+
public ItemDescriptor get(DataKind kind, String key) {
60+
if (key.equals(failingKey)) {
61+
throw new RuntimeException("store failed");
62+
}
63+
return base.get(kind, key);
64+
}
65+
66+
@Override
67+
public KeyedItems<ItemDescriptor> getAll(DataKind kind) {
68+
return base.getAll(kind);
69+
}
70+
71+
@Override
72+
public boolean isInitialized() {
73+
return true;
74+
}
75+
};
76+
}
77+
78+
// A flag whose evaluation reads the prerequisite "prereq".
79+
private static FeatureFlag flagWithPrerequisite(String key) {
80+
return flagBuilder(key).on(true).prerequisites(prerequisite("prereq", 0)).fallthroughVariation(0)
81+
.offVariation(0).variations(LDValue.of("value")).trackEvents(true).build();
82+
}
83+
84+
@Test
85+
public void exceptionWhileEvaluatingOverrideFlagKeepsMarking() {
86+
FeatureFlag flag = flagWithPrerequisite("flag").markedAsOverride();
87+
EvalResultAndFlag result = evaluatorOver(storeFailingOn("prereq", flag)).evaluate("flag", BASE_USER,
88+
LDValue.of("default"), null, InputValidatingEvaluator.NO_OP_EVALUATION_EVENT_RECORDER);
89+
90+
assertEquals(LDValue.of("default"), result.getResult().getValue());
91+
assertEquals(EvaluationReason.ErrorKind.EXCEPTION, result.getResult().getReason().getErrorKind());
92+
assertTrue(result.getResult().getReason().isOverrideAffected());
93+
assertTrue(result.getResult().isOverrideAffected());
94+
}
95+
96+
@Test
97+
public void exceptionWhileEvaluatingPlainFlagIsNotMarked() {
98+
FeatureFlag flag = flagWithPrerequisite("flag");
99+
EvalResultAndFlag result = evaluatorOver(storeFailingOn("prereq", flag)).evaluate("flag", BASE_USER,
100+
LDValue.of("default"), null, InputValidatingEvaluator.NO_OP_EVALUATION_EVENT_RECORDER);
101+
102+
assertEquals(EvaluationReason.ErrorKind.EXCEPTION, result.getResult().getReason().getErrorKind());
103+
assertFalse(result.getResult().isOverrideAffected());
104+
}
105+
52106
private static InputValidatingEvaluator evaluatorOver(ReadOnlyStore store) {
53107
return new InputValidatingEvaluator(store, null, new NoOpEventProcessor(), nullLogger);
54108
}

0 commit comments

Comments
 (0)