Skip to content

Commit 4c0eee6

Browse files
Improve error messages: split validation and include field name
- Split user ID validation into two distinct error cases: 1. Missing user ID (no valid targeting_key, user_id, or userId found) 2. Invalid type (user ID found but not a string) - Include original field name in type validation error for better debugging - Re-introduce user_id_source variable for error reporting purposes only - Enhanced error specificity: 'targeting_key must be a string, got int' vs generic message
1 parent fb2408f commit 4c0eee6

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

devcycle_python_sdk/models/user.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,28 @@ def create_user_from_context(
107107
:return: A DevCycleUser instance
108108
"""
109109
user_id = None
110+
user_id_source = None
110111

111112
if context:
112113
if context.targeting_key:
113114
user_id = context.targeting_key
115+
user_id_source = "targeting_key"
114116
elif context.attributes and "user_id" in context.attributes.keys():
115117
user_id = context.attributes["user_id"]
118+
user_id_source = "user_id"
116119
elif context.attributes and "userId" in context.attributes.keys():
117120
user_id = context.attributes["userId"]
121+
user_id_source = "userId"
118122

119-
if not user_id or not isinstance(user_id, str):
123+
if not user_id:
120124
raise TargetingKeyMissingError(
121125
"DevCycle: Evaluation context does not contain a valid targeting key, user_id, or userId attribute"
122126
)
127+
128+
if not isinstance(user_id, str):
129+
raise TargetingKeyMissingError(
130+
f"DevCycle: {user_id_source} must be a string, got {type(user_id).__name__}"
131+
)
123132

124133
user = DevCycleUser(user_id=user_id)
125134
custom_data: Dict[str, Any] = {}

0 commit comments

Comments
 (0)