Skip to content

Commit 1a22848

Browse files
authored
Improve error message for reference replacement using non-iterable types (#3)
2 parents 491b643 + c3f903e commit 1a22848

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

localstack_snapshot/snapshots/transformer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
GlobalReplacementFn = Callable[[str], str]
1717

1818

19+
class TransformerException(Exception):
20+
pass
21+
22+
1923
class TransformContext:
2024
_cache: dict
2125
replacements: list[GlobalReplacementFn]
@@ -43,6 +47,19 @@ def new_scope(self, scope: str) -> int:
4347
def _register_serialized_reference_replacement(
4448
transform_context: TransformContext, *, reference_value: str, replacement: str
4549
):
50+
# Provide a better error message for the TypeError if the reference value is not iterable (e.g., float)
51+
# Example: `TypeError: argument of type 'float' is not iterable`
52+
# Using a list would throw an AttributeError because `.replace` is not applicable.
53+
# The snapshot library currently only supports strings for reference replacements
54+
if not isinstance(reference_value, str):
55+
message = (
56+
f"The reference value {reference_value} of type {type(reference_value)} is not a string."
57+
f" Consider using `reference_replacement=False` in your transformer"
58+
f" for the replacement {replacement} because reference replacements are only supported for strings."
59+
)
60+
SNAPSHOT_LOGGER.error(message)
61+
raise TransformerException(message)
62+
4663
if '"' in reference_value:
4764
reference_value = reference_value.replace('"', '\\"')
4865

0 commit comments

Comments
 (0)