Skip to content

Commit

Permalink
fix: Finally hooks do not get called when the provider is not ready o…
Browse files Browse the repository at this point in the history
…pen-feature#424 (open-feature#425)

Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
  • Loading branch information
chrfwow authored Jan 29, 2025
1 parent 9c2ed71 commit 8f2caba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
62 changes: 32 additions & 30 deletions openfeature/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,37 +295,39 @@ def evaluate_flag_details( # noqa: PLR0915
reversed_merged_hooks = merged_hooks[:]
reversed_merged_hooks.reverse()

status = self.get_provider_status()
if status == ProviderStatus.NOT_READY:
error_hooks(
flag_type,
hook_context,
ProviderNotReadyError(),
reversed_merged_hooks,
hook_hints,
)
return FlagEvaluationDetails(
flag_key=flag_key,
value=default_value,
reason=Reason.ERROR,
error_code=ErrorCode.PROVIDER_NOT_READY,
)
if status == ProviderStatus.FATAL:
error_hooks(
flag_type,
hook_context,
ProviderFatalError(),
reversed_merged_hooks,
hook_hints,
)
return FlagEvaluationDetails(
flag_key=flag_key,
value=default_value,
reason=Reason.ERROR,
error_code=ErrorCode.PROVIDER_FATAL,
)

try:
status = self.get_provider_status()
if status == ProviderStatus.NOT_READY:
error_hooks(
flag_type,
hook_context,
ProviderNotReadyError(),
reversed_merged_hooks,
hook_hints,
)
flag_evaluation = FlagEvaluationDetails(
flag_key=flag_key,
value=default_value,
reason=Reason.ERROR,
error_code=ErrorCode.PROVIDER_NOT_READY,
)
return flag_evaluation
if status == ProviderStatus.FATAL:
error_hooks(
flag_type,
hook_context,
ProviderFatalError(),
reversed_merged_hooks,
hook_hints,
)
flag_evaluation = FlagEvaluationDetails(
flag_key=flag_key,
value=default_value,
reason=Reason.ERROR,
error_code=ErrorCode.PROVIDER_FATAL,
)
return flag_evaluation

# https://github.com/open-feature/spec/blob/main/specification/sections/03-evaluation-context.md
# Any resulting evaluation context from a before hook will overwrite
# duplicate fields defined globally, on the client, or in the invocation.
Expand Down
2 changes: 2 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def test_should_shortcircuit_if_provider_is_not_ready(
assert flag_details.reason == Reason.ERROR
assert flag_details.error_code == ErrorCode.PROVIDER_NOT_READY
spy_hook.error.assert_called_once()
spy_hook.finally_after.assert_called_once()


# Requirement 1.7.7
Expand All @@ -243,6 +244,7 @@ def test_should_shortcircuit_if_provider_is_in_irrecoverable_error_state(
assert flag_details.reason == Reason.ERROR
assert flag_details.error_code == ErrorCode.PROVIDER_FATAL
spy_hook.error.assert_called_once()
spy_hook.finally_after.assert_called_once()


def test_should_run_error_hooks_if_provider_returns_resolution_with_error_code():
Expand Down

0 comments on commit 8f2caba

Please sign in to comment.