From 06d0494331b62deb0c0ec96846ffed5ab8471e60 Mon Sep 17 00:00:00 2001 From: Robert Grassian <89157164+rgrassian-split@users.noreply.github.com> Date: Tue, 8 Nov 2022 08:35:56 -0800 Subject: [PATCH] fix: eval context fixes and new error types (#43) * eval context fixes and new error types Signed-off-by: Robert Grassian * cleanup Signed-off-by: Robert Grassian * remove eval context setter Signed-off-by: Robert Grassian Signed-off-by: Robert Grassian --- .../evaluation_context/evaluation_context.py | 6 ++-- open_feature/exception/exceptions.py | 32 +++++++++++++++++++ open_feature/flag_evaluation/error_code.py | 2 ++ open_feature/open_feature_client.py | 2 +- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/open_feature/evaluation_context/evaluation_context.py b/open_feature/evaluation_context/evaluation_context.py index bba93548..cab69762 100644 --- a/open_feature/evaluation_context/evaluation_context.py +++ b/open_feature/evaluation_context/evaluation_context.py @@ -7,7 +7,7 @@ def merge(self, ctx2: "EvaluationContext") -> "EvaluationContext": if not (self and ctx2): return self or ctx2 - self.attributes = {**self.attributes, **ctx2.attributes} - self.targeting_key = ctx2.targeting_key or self.targeting_key + attributes = {**self.attributes, **ctx2.attributes} + targeting_key = ctx2.targeting_key or self.targeting_key - return self + return EvaluationContext(targeting_key=targeting_key, attributes=attributes) diff --git a/open_feature/exception/exceptions.py b/open_feature/exception/exceptions.py index b23eee03..e0e9925c 100644 --- a/open_feature/exception/exceptions.py +++ b/open_feature/exception/exceptions.py @@ -85,3 +85,35 @@ def __init__(self, error_message: str = None): @return: the generic TypeMismatchError exception """ super().__init__(error_message, ErrorCode.TYPE_MISMATCH) + + +class TargetingKeyMissingError(OpenFeatureError): + """ + This exception should be raised when the provider requires a targeting key + but one was not provided in the evaluation context. + """ + + def __init__(self, error_message: str = None): + """ + Constructor for the TargetingKeyMissingError. The error code for this type of + exception is ErrorCode.TARGETING_KEY_MISSING. + @param error_message: a string message representing why the error has been + raised + """ + super().__init__(error_message, ErrorCode.TARGETING_KEY_MISSING) + + +class InvalidContextError(OpenFeatureError): + """ + This exception should be raised when the evaluation context does not meet provider + requirements. + """ + + def __init__(self, error_message: str = None): + """ + Constructor for the InvalidContextError. The error code for this type of + exception is ErrorCode.INVALID_CONTEXT. + @param error_message: a string message representing why the error has been + raised + """ + super().__init__(error_message, ErrorCode.INVALID_CONTEXT) diff --git a/open_feature/flag_evaluation/error_code.py b/open_feature/flag_evaluation/error_code.py index 79506ffd..96c59546 100644 --- a/open_feature/flag_evaluation/error_code.py +++ b/open_feature/flag_evaluation/error_code.py @@ -6,4 +6,6 @@ class ErrorCode(Enum): FLAG_NOT_FOUND = "FLAG_NOT_FOUND" PARSE_ERROR = "PARSE_ERROR" TYPE_MISMATCH = "TYPE_MISMATCH" + TARGETING_KEY_MISSING = "TARGETING_KEY_MISSING" + INVALID_CONTEXT = "INVALID_CONTEXT" GENERAL = "GENERAL" diff --git a/open_feature/open_feature_client.py b/open_feature/open_feature_client.py index 8be455ef..9cfeb21d 100644 --- a/open_feature/open_feature_client.py +++ b/open_feature/open_feature_client.py @@ -199,7 +199,7 @@ def evaluate_flag_details( invocation_context = before_hooks( flag_type, hook_context, merged_hooks, None ) - invocation_context.merge(ctx2=evaluation_context) + invocation_context = invocation_context.merge(ctx2=evaluation_context) # merge of: API.context, client.context, invocation.context merged_context = (