Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eval context fixes and new error types #43

Merged
merged 3 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
ajhelsby marked this conversation as resolved.
Show resolved Hide resolved
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