Skip to content

Commit

Permalink
fix: eval context fixes and new error types (#43)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
rgrassian-split authored Nov 8, 2022
1 parent 1e82122 commit 06d0494
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
6 changes: 3 additions & 3 deletions open_feature/evaluation_context/evaluation_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
32 changes: 32 additions & 0 deletions open_feature/exception/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions open_feature/flag_evaluation/error_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion open_feature/open_feature_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit 06d0494

Please sign in to comment.