-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix so error hook also runs when provider returns error code
Signed-off-by: jarebudev <[email protected]>
- Loading branch information
Showing
4 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/dev/openfeature/sdk/internal/ErrorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package dev.openfeature.sdk.internal; | ||
|
||
import dev.openfeature.sdk.ErrorCode; | ||
import dev.openfeature.sdk.exceptions.FlagNotFoundError; | ||
import dev.openfeature.sdk.exceptions.GeneralError; | ||
import dev.openfeature.sdk.exceptions.InvalidContextError; | ||
import dev.openfeature.sdk.exceptions.OpenFeatureError; | ||
import dev.openfeature.sdk.exceptions.ParseError; | ||
import dev.openfeature.sdk.exceptions.ProviderNotReadyError; | ||
import dev.openfeature.sdk.exceptions.TargetingKeyMissingError; | ||
import dev.openfeature.sdk.exceptions.TypeMismatchError; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@SuppressWarnings("checkstyle:MissingJavadocType") | ||
@UtilityClass | ||
public class ErrorUtils { | ||
|
||
/** | ||
* Creates an Error for the specific error code. | ||
* @param errorCode the ErrorCode to use | ||
* @param errorMessage the error message to include in the returned error | ||
* @return the specific OpenFeatureError for the errorCode | ||
*/ | ||
public static OpenFeatureError instantiateErrorByErrorCode(ErrorCode errorCode, String errorMessage) { | ||
switch (errorCode) { | ||
case FLAG_NOT_FOUND: | ||
return new FlagNotFoundError(errorMessage); | ||
case PARSE_ERROR: | ||
return new ParseError(errorMessage); | ||
case TYPE_MISMATCH: | ||
return new TypeMismatchError(errorMessage); | ||
case TARGETING_KEY_MISSING: | ||
return new TargetingKeyMissingError(errorMessage); | ||
case INVALID_CONTEXT: | ||
return new InvalidContextError(errorMessage); | ||
case PROVIDER_NOT_READY: | ||
return new ProviderNotReadyError(errorMessage); | ||
default: | ||
return new GeneralError(errorMessage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/test/java/dev/openfeature/sdk/internal/ErrorUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dev.openfeature.sdk.internal; | ||
|
||
import dev.openfeature.sdk.ErrorCode; | ||
import dev.openfeature.sdk.exceptions.FlagNotFoundError; | ||
import dev.openfeature.sdk.exceptions.GeneralError; | ||
import dev.openfeature.sdk.exceptions.InvalidContextError; | ||
import dev.openfeature.sdk.exceptions.OpenFeatureError; | ||
import dev.openfeature.sdk.exceptions.ParseError; | ||
import dev.openfeature.sdk.exceptions.ProviderNotReadyError; | ||
import dev.openfeature.sdk.exceptions.TargetingKeyMissingError; | ||
import dev.openfeature.sdk.exceptions.TypeMismatchError; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
import org.junit.jupiter.params.provider.ArgumentsSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
|
||
class ErrorUtilsTest { | ||
|
||
@ParameterizedTest | ||
@DisplayName("should produce correct exception for a provided ErrorCode") | ||
@ArgumentsSource(ErrorCodeTestParameters.class) | ||
void shouldProduceCorrectExceptionForErrorCode(ErrorCode errorCode, Class<? extends OpenFeatureError> exception) { | ||
|
||
String errorMessage = "error message"; | ||
OpenFeatureError openFeatureError = ErrorUtils.instantiateErrorByErrorCode(errorCode, errorMessage); | ||
assertInstanceOf(exception, openFeatureError); | ||
assertThat(openFeatureError.getMessage()).isEqualTo(errorMessage); | ||
assertThat(openFeatureError.getErrorCode()).isEqualByComparingTo(errorCode); | ||
} | ||
|
||
static class ErrorCodeTestParameters implements ArgumentsProvider { | ||
|
||
@Override | ||
public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | ||
return Stream.of( | ||
Arguments.of(ErrorCode.GENERAL, GeneralError.class), | ||
Arguments.of(ErrorCode.FLAG_NOT_FOUND, FlagNotFoundError.class), | ||
Arguments.of(ErrorCode.PROVIDER_NOT_READY, ProviderNotReadyError.class), | ||
Arguments.of(ErrorCode.INVALID_CONTEXT, InvalidContextError.class), | ||
Arguments.of(ErrorCode.PARSE_ERROR, ParseError.class), | ||
Arguments.of(ErrorCode.TARGETING_KEY_MISSING, TargetingKeyMissingError.class), | ||
Arguments.of(ErrorCode.TYPE_MISMATCH, TypeMismatchError.class) | ||
); | ||
} | ||
} | ||
} |