Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: error resolution flow control without exceptions #1095

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions src/main/java/dev/openfeature/sdk/OpenFeatureClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* You should not instantiate this or reference this class.
* Use the dev.openfeature.sdk.Client interface instead.
* @see Client
*
*
* @deprecated // TODO: eventually we will make this non-public. See issue #872
*/
@Slf4j
Expand Down Expand Up @@ -132,7 +132,11 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key

details = FlagEvaluationDetails.from(providerEval, key);
if (details.getErrorCode() != null) {
throw ExceptionUtils.instantiateErrorByErrorCode(details.getErrorCode(), details.getErrorMessage());
OpenFeatureError error = ExceptionUtils.instantiateErrorByErrorCode(
details.getErrorCode(),
details.getErrorMessage());
details.setValue(defaultValue);
aepfli marked this conversation as resolved.
Show resolved Hide resolved
hookSupport.errorHooks(type, afterHookContext, error, mergedHooks, hints);
} else {
hookSupport.afterHooks(type, afterHookContext, details, mergedHooks, hints);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dev.openfeature.sdk;

import dev.openfeature.sdk.exceptions.FlagNotFoundError;

public class AlwaysBrokenWithDetailsProvider implements FeatureProvider {

@Override
public Metadata getMetadata() {
return () -> {
throw new FlagNotFoundError(TestConstants.BROKEN_MESSAGE);
};
}

@Override
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) {
return ProviderEvaluation.<Boolean>builder()
.errorMessage(TestConstants.BROKEN_MESSAGE)
.errorCode(ErrorCode.FLAG_NOT_FOUND)
.build();
}

@Override
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) {
return ProviderEvaluation.<String>builder()
.errorMessage(TestConstants.BROKEN_MESSAGE)
.errorCode(ErrorCode.FLAG_NOT_FOUND)
.build();
}

@Override
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) {
return ProviderEvaluation.<Integer>builder()
.errorMessage(TestConstants.BROKEN_MESSAGE)
.errorCode(ErrorCode.FLAG_NOT_FOUND)
.build();
}

@Override
public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) {
return ProviderEvaluation.<Double>builder()
.errorMessage(TestConstants.BROKEN_MESSAGE)
.errorCode(ErrorCode.FLAG_NOT_FOUND)
.build();
}

@Override
public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext invocationContext) {
return ProviderEvaluation.<Value>builder()
.errorMessage(TestConstants.BROKEN_MESSAGE)
.errorCode(ErrorCode.FLAG_NOT_FOUND)
.build();
}
}
21 changes: 19 additions & 2 deletions src/test/java/dev/openfeature/sdk/FlagEvaluationSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,27 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
@Test void broken_provider() {
FeatureProviderTestUtils.setFeatureProvider(new AlwaysBrokenProvider());
Client c = api.getClient();
assertFalse(c.getBooleanValue("key", false));
FlagEvaluationDetails<Boolean> details = c.getBooleanDetails("key", false);
boolean defaultValue = false;
assertFalse(c.getBooleanValue("key", defaultValue));
FlagEvaluationDetails<Boolean> details = c.getBooleanDetails("key", defaultValue);
assertEquals(ErrorCode.FLAG_NOT_FOUND, details.getErrorCode());
assertEquals(TestConstants.BROKEN_MESSAGE, details.getErrorMessage());
assertEquals(defaultValue, details.getValue());
}

@Specification(number="1.4.8", text="In cases of abnormal execution, the `evaluation details` structure's `error code` field **MUST** contain an `error code`.")
@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.")
@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.")
@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.")
@Test void broken_provider_withDetails() {
FeatureProviderTestUtils.setFeatureProvider(new AlwaysBrokenWithDetailsProvider());
Client c = api.getClient();
boolean defaultValue = false;
assertFalse(c.getBooleanValue("key", defaultValue));
FlagEvaluationDetails<Boolean> details = c.getBooleanDetails("key", defaultValue);
assertEquals(ErrorCode.FLAG_NOT_FOUND, details.getErrorCode());
assertEquals(TestConstants.BROKEN_MESSAGE, details.getErrorMessage());
assertEquals(defaultValue, details.getValue());
}

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