Skip to content

Commit

Permalink
Improve error message for reference replacement using non-iterable ty…
Browse files Browse the repository at this point in the history
…pes (#3)
  • Loading branch information
joe4dev authored Mar 13, 2024
2 parents 491b643 + c3f903e commit 1a22848
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions localstack_snapshot/snapshots/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
GlobalReplacementFn = Callable[[str], str]


class TransformerException(Exception):
pass


class TransformContext:
_cache: dict
replacements: list[GlobalReplacementFn]
Expand Down Expand Up @@ -43,6 +47,19 @@ def new_scope(self, scope: str) -> int:
def _register_serialized_reference_replacement(
transform_context: TransformContext, *, reference_value: str, replacement: str
):
# Provide a better error message for the TypeError if the reference value is not iterable (e.g., float)
# Example: `TypeError: argument of type 'float' is not iterable`
# Using a list would throw an AttributeError because `.replace` is not applicable.
# The snapshot library currently only supports strings for reference replacements
if not isinstance(reference_value, str):
message = (
f"The reference value {reference_value} of type {type(reference_value)} is not a string."
f" Consider using `reference_replacement=False` in your transformer"
f" for the replacement {replacement} because reference replacements are only supported for strings."
)
SNAPSHOT_LOGGER.error(message)
raise TransformerException(message)

if '"' in reference_value:
reference_value = reference_value.replace('"', '\\"')

Expand Down

0 comments on commit 1a22848

Please sign in to comment.