Skip to content

Commit 0f197a0

Browse files
committed
fix so error hook also runs when provider returns error code
Signed-off-by: jarebudev <[email protected]>
1 parent c3c0fd2 commit 0f197a0

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import dev.openfeature.sdk.exceptions.OpenFeatureError;
1212
import dev.openfeature.sdk.internal.AutoCloseableLock;
1313
import dev.openfeature.sdk.internal.AutoCloseableReentrantReadWriteLock;
14+
import dev.openfeature.sdk.internal.ErrorUtils;
1415
import dev.openfeature.sdk.internal.ObjectUtils;
1516
import lombok.Getter;
1617
import lombok.extern.slf4j.Slf4j;
@@ -125,7 +126,12 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key
125126
defaultValue, provider, mergedCtx);
126127

127128
details = FlagEvaluationDetails.from(providerEval, key);
128-
hookSupport.afterHooks(type, hookCtx, details, mergedHooks, hints);
129+
if (details.getErrorCode() != null) {
130+
Exception e = ErrorUtils.instantiateErrorByErrorCode(details.getErrorCode(), details.getErrorMessage());
131+
hookSupport.errorHooks(type, hookCtx, e, mergedHooks, hints);
132+
} else {
133+
hookSupport.afterHooks(type, hookCtx, details, mergedHooks, hints);
134+
}
129135
} catch (Exception e) {
130136
log.error("Unable to correctly evaluate flag with key '{}'", key, e);
131137
if (details == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dev.openfeature.sdk.internal;
2+
3+
import dev.openfeature.sdk.ErrorCode;
4+
import dev.openfeature.sdk.exceptions.FlagNotFoundError;
5+
import dev.openfeature.sdk.exceptions.GeneralError;
6+
import dev.openfeature.sdk.exceptions.InvalidContextError;
7+
import dev.openfeature.sdk.exceptions.OpenFeatureError;
8+
import dev.openfeature.sdk.exceptions.ParseError;
9+
import dev.openfeature.sdk.exceptions.ProviderNotReadyError;
10+
import dev.openfeature.sdk.exceptions.TargetingKeyMissingError;
11+
import dev.openfeature.sdk.exceptions.TypeMismatchError;
12+
import lombok.experimental.UtilityClass;
13+
14+
@SuppressWarnings("checkstyle:MissingJavadocType")
15+
@UtilityClass
16+
public class ErrorUtils {
17+
18+
/**
19+
* Creates an Error for the specific error code.
20+
* @param errorCode the ErrorCode to use
21+
* @param errorMessage the error message to include in the returned error
22+
* @return the specific OpenFeatureError for the errorCode
23+
*/
24+
public static OpenFeatureError instantiateErrorByErrorCode(ErrorCode errorCode, String errorMessage) {
25+
switch (errorCode) {
26+
case FLAG_NOT_FOUND:
27+
return new FlagNotFoundError(errorMessage);
28+
case PARSE_ERROR:
29+
return new ParseError(errorMessage);
30+
case TYPE_MISMATCH:
31+
return new TypeMismatchError(errorMessage);
32+
case TARGETING_KEY_MISSING:
33+
return new TargetingKeyMissingError(errorMessage);
34+
case INVALID_CONTEXT:
35+
return new InvalidContextError(errorMessage);
36+
case PROVIDER_NOT_READY:
37+
return new ProviderNotReadyError(errorMessage);
38+
default:
39+
return new GeneralError(errorMessage);
40+
}
41+
}
42+
}

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

+37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.openfeature.sdk;
22

3+
import dev.openfeature.sdk.exceptions.FlagNotFoundError;
34
import dev.openfeature.sdk.fixtures.HookFixtures;
45
import dev.openfeature.sdk.testutils.FeatureProviderTestUtils;
56
import lombok.SneakyThrows;
@@ -19,12 +20,14 @@
1920
import static org.assertj.core.api.Assertions.assertThatCode;
2021
import static org.assertj.core.api.Assertions.fail;
2122
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2224
import static org.junit.jupiter.api.Assertions.assertNotNull;
2325
import static org.junit.jupiter.api.Assertions.assertTrue;
2426
import static org.mockito.ArgumentMatchers.any;
2527
import static org.mockito.Mockito.doThrow;
2628
import static org.mockito.Mockito.inOrder;
2729
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.never;
2831
import static org.mockito.Mockito.times;
2932
import static org.mockito.Mockito.verify;
3033
import static org.mockito.Mockito.when;
@@ -180,6 +183,40 @@ void emptyApiHooks() {
180183
verify(h, times(0)).error(any(), any(), any());
181184
}
182185

186+
187+
@Test void error_hook_must_run_if_resolution_details_returns_an_error_code() {
188+
189+
String errorMessage = "not found...";
190+
191+
EvaluationContext invocationCtx = new ImmutableContext();
192+
Hook<Boolean> hook = mockBooleanHook();
193+
FeatureProvider provider = mock(FeatureProvider.class);
194+
when(provider.getBooleanEvaluation(any(), any(), any())).thenReturn(ProviderEvaluation.<Boolean>builder()
195+
.errorCode(ErrorCode.FLAG_NOT_FOUND)
196+
.errorMessage(errorMessage)
197+
.build());
198+
199+
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
200+
FeatureProviderTestUtils.setFeatureProvider(provider);
201+
Client client = api.getClient();
202+
client.getBooleanValue("key", false, invocationCtx,
203+
FlagEvaluationOptions.builder()
204+
.hook(hook)
205+
.build());
206+
207+
ArgumentCaptor<Exception> captor = ArgumentCaptor.forClass(Exception.class);
208+
209+
verify(hook, times(1)).before(any(), any());
210+
verify(hook, times(1)).error(any(), captor.capture(), any());
211+
verify(hook, times(1)).finallyAfter(any(), any());
212+
verify(hook, never()).after(any(),any(), any());
213+
214+
Exception exception = captor.getValue();
215+
assertEquals(errorMessage, exception.getMessage());
216+
assertInstanceOf(FlagNotFoundError.class, exception);
217+
}
218+
219+
183220
@Specification(number="4.3.6", text="The after stage MUST run after flag resolution occurs. It accepts a hook context (required), flag evaluation details (required) and hook hints (optional). It has no return value.")
184221
@Specification(number="4.3.7", text="The error hook MUST run when errors are encountered in the before stage, the after stage or during flag resolution. It accepts hook context (required), exception representing what went wrong (required), and hook hints (optional). It has no return value.")
185222
@Specification(number="4.3.8", text="The finally hook MUST run after the before, after, and error stages. It accepts a hook context (required) and hook hints (optional). There is no return value.")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package dev.openfeature.sdk.internal;
2+
3+
import dev.openfeature.sdk.ErrorCode;
4+
import dev.openfeature.sdk.exceptions.FlagNotFoundError;
5+
import dev.openfeature.sdk.exceptions.GeneralError;
6+
import dev.openfeature.sdk.exceptions.InvalidContextError;
7+
import dev.openfeature.sdk.exceptions.OpenFeatureError;
8+
import dev.openfeature.sdk.exceptions.ParseError;
9+
import dev.openfeature.sdk.exceptions.ProviderNotReadyError;
10+
import dev.openfeature.sdk.exceptions.TargetingKeyMissingError;
11+
import dev.openfeature.sdk.exceptions.TypeMismatchError;
12+
import org.junit.jupiter.api.DisplayName;
13+
import org.junit.jupiter.api.extension.ExtensionContext;
14+
import org.junit.jupiter.params.ParameterizedTest;
15+
import org.junit.jupiter.params.provider.Arguments;
16+
import org.junit.jupiter.params.provider.ArgumentsProvider;
17+
import org.junit.jupiter.params.provider.ArgumentsSource;
18+
19+
import java.util.stream.Stream;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
23+
24+
class ErrorUtilsTest {
25+
26+
@ParameterizedTest
27+
@DisplayName("should produce correct exception for a provided ErrorCode")
28+
@ArgumentsSource(ErrorCodeTestParameters.class)
29+
void shouldProduceCorrectExceptionForErrorCode(ErrorCode errorCode, Class<? extends OpenFeatureError> exception) {
30+
31+
String errorMessage = "error message";
32+
OpenFeatureError openFeatureError = ErrorUtils.instantiateErrorByErrorCode(errorCode, errorMessage);
33+
assertInstanceOf(exception, openFeatureError);
34+
assertThat(openFeatureError.getMessage()).isEqualTo(errorMessage);
35+
assertThat(openFeatureError.getErrorCode()).isEqualByComparingTo(errorCode);
36+
}
37+
38+
static class ErrorCodeTestParameters implements ArgumentsProvider {
39+
40+
@Override
41+
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
42+
return Stream.of(
43+
Arguments.of(ErrorCode.GENERAL, GeneralError.class),
44+
Arguments.of(ErrorCode.FLAG_NOT_FOUND, FlagNotFoundError.class),
45+
Arguments.of(ErrorCode.PROVIDER_NOT_READY, ProviderNotReadyError.class),
46+
Arguments.of(ErrorCode.INVALID_CONTEXT, InvalidContextError.class),
47+
Arguments.of(ErrorCode.PARSE_ERROR, ParseError.class),
48+
Arguments.of(ErrorCode.TARGETING_KEY_MISSING, TargetingKeyMissingError.class),
49+
Arguments.of(ErrorCode.TYPE_MISMATCH, TypeMismatchError.class)
50+
);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)