Skip to content

CFn: improve error message for invalid ref #10149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 31, 2024

Conversation

simonrw
Copy link
Contributor

@simonrw simonrw commented Jan 31, 2024

Motivation

When we cannot resolve a Ref - specifically if it is targeting a non-existent resource or parameter, we currently print "Should be detected earlier." which is not very useful. Instead, since this is a programming error from the creator of the template, we should be more helpful and print out the ref that cannot be resolved.

Changes

  • Increase clarity of error message when not finding a ref
  • Add test to ensure the ref target is in the error message
  • Add more clarity to additional error messages

@simonrw simonrw added the semver: patch Non-breaking changes which can be included in patch releases label Jan 31, 2024
@simonrw simonrw self-assigned this Jan 31, 2024
@dominikschubert dominikschubert marked this pull request as ready for review January 31, 2024 09:58
Copy link
Member

@dominikschubert dominikschubert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks for jumping on this. Minor change but will be super useful for anyone debugging their Stacks!

@@ -169,7 +169,9 @@ def resolve_ref(
# resource
resource = resources.get(ref)
if not resource:
raise Exception("Should be detected earlier.")
raise Exception(
f"Resource target for `!Ref {ref}` could not be found. Is there a resource with name {ref} in your stack?"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*with the logical resource id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to not be too technical in this message. I think referring to "name" is more actionable, rather than "logical resource id" which is technically correct.

raise DependencyNotYetSatisfied(resource_ids=resource_logical_id, message="")
raise DependencyNotYetSatisfied(
resource_ids=resource_logical_id,
message=f"Could not resolve attribute {attribute_name} on resource {resource_logical_id}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: enclosing replaced values with quotation marks might make this a bit more readable

@@ -361,7 +366,7 @@ def _resolve_refs_recursively(
none_values = [v for v in join_values if v is None]
if none_values:
raise Exception(
"Cannot resolve CF fn::Join %s due to null values: %s" % (value, join_values)
f"Cannot resolve CF Fn::Join {value} due to null values: {join_values}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we're using the full canonical version while using !Ref above. Would suggest keeping this consistent. (Personal preference would be slightly towards canonical)



@markers.aws.only_localstack
def test_useful_error_when_invalid_ref(deploy_cfn_template):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to add a test here tp avoid us messing it up when refactoring on the future. We might want to pull out tests like this from the file in the future though.

@coveralls
Copy link

coveralls commented Jan 31, 2024

Coverage Status

coverage: 83.812% (-0.03%) from 83.841%
when pulling 7ceb10b on fix/cfn/no-ref-message-improvement
into 4a5f76e on master.

Copy link

LocalStack Community integration with Pro

    2 files  ±0      2 suites  ±0   1h 20m 41s ⏱️ +13s
2 617 tests +1  2 369 ✅ +1  248 💤 ±0  0 ❌ ±0 
2 619 runs  +1  2 369 ✅ +1  250 💤 ±0  0 ❌ ±0 

Results for commit 7ceb10b. ± Comparison against base commit 4a5f76e.

@simonrw simonrw merged commit 46d2dae into master Jan 31, 2024
@simonrw simonrw deleted the fix/cfn/no-ref-message-improvement branch January 31, 2024 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver: patch Non-breaking changes which can be included in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants