Skip to content

Commit 06d0494

Browse files
fix: eval context fixes and new error types (#43)
* eval context fixes and new error types Signed-off-by: Robert Grassian <[email protected]> * cleanup Signed-off-by: Robert Grassian <[email protected]> * remove eval context setter Signed-off-by: Robert Grassian <[email protected]> Signed-off-by: Robert Grassian <[email protected]>
1 parent 1e82122 commit 06d0494

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

open_feature/evaluation_context/evaluation_context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def merge(self, ctx2: "EvaluationContext") -> "EvaluationContext":
77
if not (self and ctx2):
88
return self or ctx2
99

10-
self.attributes = {**self.attributes, **ctx2.attributes}
11-
self.targeting_key = ctx2.targeting_key or self.targeting_key
10+
attributes = {**self.attributes, **ctx2.attributes}
11+
targeting_key = ctx2.targeting_key or self.targeting_key
1212

13-
return self
13+
return EvaluationContext(targeting_key=targeting_key, attributes=attributes)

open_feature/exception/exceptions.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,35 @@ def __init__(self, error_message: str = None):
8585
@return: the generic TypeMismatchError exception
8686
"""
8787
super().__init__(error_message, ErrorCode.TYPE_MISMATCH)
88+
89+
90+
class TargetingKeyMissingError(OpenFeatureError):
91+
"""
92+
This exception should be raised when the provider requires a targeting key
93+
but one was not provided in the evaluation context.
94+
"""
95+
96+
def __init__(self, error_message: str = None):
97+
"""
98+
Constructor for the TargetingKeyMissingError. The error code for this type of
99+
exception is ErrorCode.TARGETING_KEY_MISSING.
100+
@param error_message: a string message representing why the error has been
101+
raised
102+
"""
103+
super().__init__(error_message, ErrorCode.TARGETING_KEY_MISSING)
104+
105+
106+
class InvalidContextError(OpenFeatureError):
107+
"""
108+
This exception should be raised when the evaluation context does not meet provider
109+
requirements.
110+
"""
111+
112+
def __init__(self, error_message: str = None):
113+
"""
114+
Constructor for the InvalidContextError. The error code for this type of
115+
exception is ErrorCode.INVALID_CONTEXT.
116+
@param error_message: a string message representing why the error has been
117+
raised
118+
"""
119+
super().__init__(error_message, ErrorCode.INVALID_CONTEXT)

open_feature/flag_evaluation/error_code.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ class ErrorCode(Enum):
66
FLAG_NOT_FOUND = "FLAG_NOT_FOUND"
77
PARSE_ERROR = "PARSE_ERROR"
88
TYPE_MISMATCH = "TYPE_MISMATCH"
9+
TARGETING_KEY_MISSING = "TARGETING_KEY_MISSING"
10+
INVALID_CONTEXT = "INVALID_CONTEXT"
911
GENERAL = "GENERAL"

open_feature/open_feature_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def evaluate_flag_details(
199199
invocation_context = before_hooks(
200200
flag_type, hook_context, merged_hooks, None
201201
)
202-
invocation_context.merge(ctx2=evaluation_context)
202+
invocation_context = invocation_context.merge(ctx2=evaluation_context)
203203

204204
# merge of: API.context, client.context, invocation.context
205205
merged_context = (

0 commit comments

Comments
 (0)