Skip to content

Commit c5e0646

Browse files
committed
fix: Additional except handling in DataClasses str
When Data Classes are constructed, they accept a raw_data object which may not be compatible with all operations available in the Data Class (such as JSON decoding non-JSON strings). The DictWrapper._str_helper() method attempts to call all properties and will encounter cases where properties are either not present, or raise exceptions due to a mismatch in expectations of what the data should be versus what is actually present. Additional exception handling is needed to provide useful outputs for debugging while also presenting which properties cause exceptions when called. If exceptions occur in the list iterator, then we ignore any transformations and keep the item as-is, as transforming to an exception message doesn't provide any context (as there is no dictionary key bound to the item). Practially this means the item will maintain it's class@address output when printed.
1 parent d5bf3db commit c5e0646

File tree

1 file changed

+15
-10
lines changed
  • aws_lambda_powertools/utilities/data_classes

1 file changed

+15
-10
lines changed

aws_lambda_powertools/utilities/data_classes/common.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,21 @@ def _str_helper(self) -> Dict[str, Any]:
5353
if property_key in sensitive_properties:
5454
result[property_key] = "[SENSITIVE]"
5555
else:
56-
property_value = getattr(self, property_key)
57-
if issubclass(property_value.__class__, DictWrapper):
58-
result[property_key] = property_value._str_helper()
59-
elif isinstance(property_value, list):
60-
result[property_key] = [
61-
item._str_helper() if issubclass(item.__class__, DictWrapper) else item
62-
for item in property_value
63-
]
64-
else:
65-
result[property_key] = str(property_value)
56+
try:
57+
property_value = getattr(self, property_key)
58+
if issubclass(property_value.__class__, DictWrapper):
59+
result[property_key] = property_value._str_helper()
60+
elif isinstance(property_value, list):
61+
for seq, item in enumerate(result[property_key]):
62+
if issubclass(item.__class__, DictWrapper):
63+
try:
64+
result[property_key][seq] = item._str_helper()
65+
except Exception:
66+
pass
67+
else:
68+
result[property_key] = str(property_value)
69+
except Exception as e:
70+
result[property_key] = f"[EXCEPTION {type(e)}]"
6671

6772
return result
6873

0 commit comments

Comments
 (0)