Skip to content

Commit 4fb8e6b

Browse files
committed
Allowing flowcontrol with out exceptions
So far we used exception to handle our flowcontrol, but Exceptions are costly. In the end we enriched our evaluation Details with errorCode and errorMessage. This can be also handled by the providers if desired, to reduce the execution footprint in errornous cases, which do not have to be exceptions. Eg FlagNotFound - it might be the case, but in performance critical environments, an exception rather than a normal return, can cause overhead and can be already too costly. Signed-off-by: Simon Schrottner <simon.schrottner@dynatrace.com>
1 parent 082f574 commit 4fb8e6b

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

src/main/java/dev/openfeature/sdk/OpenFeatureClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* You should not instantiate this or reference this class.
2222
* Use the dev.openfeature.sdk.Client interface instead.
2323
* @see Client
24-
*
24+
*
2525
* @deprecated // TODO: eventually we will make this non-public. See issue #872
2626
*/
2727
@Slf4j
@@ -132,7 +132,11 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key
132132

133133
details = FlagEvaluationDetails.from(providerEval, key);
134134
if (details.getErrorCode() != null) {
135-
throw ExceptionUtils.instantiateErrorByErrorCode(details.getErrorCode(), details.getErrorMessage());
135+
OpenFeatureError error = ExceptionUtils.instantiateErrorByErrorCode(
136+
details.getErrorCode(),
137+
details.getErrorMessage());
138+
details.setValue(defaultValue);
139+
hookSupport.errorHooks(type, afterHookContext, error, mergedHooks, hints);
136140
} else {
137141
hookSupport.afterHooks(type, afterHookContext, details, mergedHooks, hints);
138142
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package dev.openfeature.sdk;
2+
3+
import dev.openfeature.sdk.exceptions.FlagNotFoundError;
4+
5+
public class AlwaysBrokenWithDetailsProvider implements FeatureProvider {
6+
7+
@Override
8+
public Metadata getMetadata() {
9+
return () -> {
10+
throw new FlagNotFoundError(TestConstants.BROKEN_MESSAGE);
11+
};
12+
}
13+
14+
@Override
15+
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) {
16+
return ProviderEvaluation.<Boolean>builder()
17+
.errorMessage(TestConstants.BROKEN_MESSAGE)
18+
.errorCode(ErrorCode.FLAG_NOT_FOUND)
19+
.build();
20+
}
21+
22+
@Override
23+
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) {
24+
return ProviderEvaluation.<String>builder()
25+
.errorMessage(TestConstants.BROKEN_MESSAGE)
26+
.errorCode(ErrorCode.FLAG_NOT_FOUND)
27+
.build();
28+
}
29+
30+
@Override
31+
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) {
32+
return ProviderEvaluation.<Integer>builder()
33+
.errorMessage(TestConstants.BROKEN_MESSAGE)
34+
.errorCode(ErrorCode.FLAG_NOT_FOUND)
35+
.build();
36+
}
37+
38+
@Override
39+
public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) {
40+
return ProviderEvaluation.<Double>builder()
41+
.errorMessage(TestConstants.BROKEN_MESSAGE)
42+
.errorCode(ErrorCode.FLAG_NOT_FOUND)
43+
.build();
44+
}
45+
46+
@Override
47+
public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext invocationContext) {
48+
return ProviderEvaluation.<Value>builder()
49+
.errorMessage(TestConstants.BROKEN_MESSAGE)
50+
.errorCode(ErrorCode.FLAG_NOT_FOUND)
51+
.build();
52+
}
53+
}

src/test/java/dev/openfeature/sdk/FlagEvaluationSpecTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,27 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
259259
@Test void broken_provider() {
260260
FeatureProviderTestUtils.setFeatureProvider(new AlwaysBrokenProvider());
261261
Client c = api.getClient();
262-
assertFalse(c.getBooleanValue("key", false));
263-
FlagEvaluationDetails<Boolean> details = c.getBooleanDetails("key", false);
262+
boolean defaultValue = false;
263+
assertFalse(c.getBooleanValue("key", defaultValue));
264+
FlagEvaluationDetails<Boolean> details = c.getBooleanDetails("key", defaultValue);
264265
assertEquals(ErrorCode.FLAG_NOT_FOUND, details.getErrorCode());
265266
assertEquals(TestConstants.BROKEN_MESSAGE, details.getErrorMessage());
267+
assertEquals(defaultValue, details.getValue());
268+
}
269+
270+
@Specification(number="1.4.8", text="In cases of abnormal execution, the `evaluation details` structure's `error code` field **MUST** contain an `error code`.")
271+
@Specification(number="1.4.9", text="In cases of abnormal execution (network failure, unhandled error, etc) the `reason` field in the `evaluation details` SHOULD indicate an error.")
272+
@Specification(number="1.4.10", text="Methods, functions, or operations on the client MUST NOT throw exceptions, or otherwise abnormally terminate. Flag evaluation calls must always return the `default value` in the event of abnormal execution. Exceptions include functions or methods for the purposes for configuration or setup.")
273+
@Specification(number="1.4.13", text="In cases of abnormal execution, the `evaluation details` structure's `error message` field **MAY** contain a string containing additional details about the nature of the error.")
274+
@Test void broken_provider_withDetails() {
275+
FeatureProviderTestUtils.setFeatureProvider(new AlwaysBrokenWithDetailsProvider());
276+
Client c = api.getClient();
277+
boolean defaultValue = false;
278+
assertFalse(c.getBooleanValue("key", defaultValue));
279+
FlagEvaluationDetails<Boolean> details = c.getBooleanDetails("key", defaultValue);
280+
assertEquals(ErrorCode.FLAG_NOT_FOUND, details.getErrorCode());
281+
assertEquals(TestConstants.BROKEN_MESSAGE, details.getErrorMessage());
282+
assertEquals(defaultValue, details.getValue());
266283
}
267284

268285
@Specification(number="1.4.11", text="Methods, functions, or operations on the client SHOULD NOT write log messages.")

0 commit comments

Comments
 (0)