Skip to content

Commit a6d9771

Browse files
update: new class method to handle optimizely error decisions
1 parent 73a2802 commit a6d9771

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

optimizely/decision/optimizely_decision.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,25 @@ def as_json(self) -> dict[str, Any]:
4848
'user_context': self.user_context.as_json() if self.user_context else None,
4949
'reasons': self.reasons
5050
}
51+
52+
@classmethod
53+
def new_error_decision(cls, key: str, user: OptimizelyUserContext, reasons: list[str]) -> OptimizelyDecision:
54+
"""Create a new OptimizelyDecision representing an error state.
55+
56+
Args:
57+
key: The flag key
58+
user: The user context
59+
reasons: List of reasons explaining the error
60+
61+
Returns:
62+
OptimizelyDecision with error state values
63+
"""
64+
return cls(
65+
variation_key=None,
66+
enabled=False,
67+
variables={},
68+
rule_key=None,
69+
flag_key=key,
70+
user_context=user,
71+
reasons=[reasons[-1]] if reasons else []
72+
)

optimizely/decision_service.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,9 @@ def _get_decision_for_cmab_experiment(
162162
"reasons": [],
163163
}
164164
except Exception as e:
165-
error_message = Errors.CMAB_FETCH_FAILED_DETAILED.format(
166-
experiment.key,
167-
str(e)
168-
)
165+
error_message = Errors.CMAB_FETCH_FAILED_DETAILED.format(experiment.key)
169166
if self.logger:
170-
self.logger.error(error_message)
167+
self.logger.error(f"{error_message} - {str(e)}")
171168
return {
172169
"error": True,
173170
"result": None,

optimizely/helpers/enums.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Errors:
129129
MISSING_SDK_KEY: Final = 'SDK key not provided/cannot be found in the datafile.'
130130
CMAB_FETCH_FAILED: Final = 'CMAB decision fetch failed with status: {}'
131131
INVALID_CMAB_FETCH_RESPONSE: Final = 'Invalid CMAB fetch response'
132-
CMAB_FETCH_FAILED_DETAILED: Final = 'Failed to fetch CMAB decision for experiment key "{}" - {}'
132+
CMAB_FETCH_FAILED_DETAILED: Final = 'Failed to fetch CMAB data for experiment {}'
133133

134134

135135
class ForcedDecisionLogs:

optimizely/optimizely.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,10 +1380,15 @@ def _decide_for_keys(
13801380
for i in range(0, len(flags_without_forced_decision)):
13811381
decision = decision_list[i]['decision']
13821382
reasons = decision_list[i]['reasons']
1383-
# Can catch errors now. Not used as decision logic implicitly handles error decision.
1384-
# Will be required for impression events
1385-
# error = decision_list[i]['error']
1383+
error = decision_list[i]['error']
13861384
flag_key = flags_without_forced_decision[i].key
1385+
# store error decision against key and remove key from valid keys
1386+
if error:
1387+
optimizely_decision = OptimizelyDecision.new_error_decision(flags_without_forced_decision[i].key,
1388+
user_context, reasons)
1389+
decisions[flag_key] = optimizely_decision
1390+
if flag_key in valid_keys:
1391+
valid_keys.remove(flag_key)
13871392
flag_decisions[flag_key] = decision
13881393
decision_reasons_dict[flag_key] += reasons
13891394

0 commit comments

Comments
 (0)