Skip to content

Chore: Provide additional info in jinja rendering errors#5318

Merged
themisvaltinos merged 5 commits intomainfrom
themis/jinja
Sep 9, 2025
Merged

Chore: Provide additional info in jinja rendering errors#5318
themisvaltinos merged 5 commits intomainfrom
themis/jinja

Conversation

@themisvaltinos
Copy link
Contributor

@themisvaltinos themisvaltinos commented Sep 8, 2025

This PR adds a best effort frame analysis to identify the specific macro invocation that triggered the jinja error.

Currently, when the macro rendering fails, the error message only references the model containing the macro, but not which macro led to this error:
Screenshot 2025-09-09 at 12 35 55

With this change the error message is augmented with the originating macro to improve debugging:
Screenshot 2025-09-09 at 12 42 31

if isinstance(ex, UndefinedError):
try:
_, _, exc_traceback = exc_info()
for frame, _ in walk_tb(exc_traceback):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess this is necessary because Jinja gets turned into a Python script and run.

Are you able to add a test that exercises this codepath and shows the expected results?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes added tests for native and dbt project for this

@themisvaltinos
Copy link
Contributor Author

thanks for the review @erindru and for the offline feedback @georgesittas and @crericha! addressed comments as discussed:

  • removed the macro definition from the output to make it less noisy and only added the macro name
  • moved the extraction logic into a helper method in jinja utils
  • added unit tests

@themisvaltinos themisvaltinos requested a review from a team September 9, 2025 10:27
@georgesittas
Copy link
Contributor

Quick heads up about https://github.com/TobikoData/sqlmesh/pull/5330– it'll cause conflicts with this one.

Comment on lines +250 to +247
error_msg = f"Could not render or parse jinja for '{self._path}'.\n" + (
extract_error_details(ex) or f"{ex}"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: can we just fallback to str(ex) within extract_error_details instead of needing an or here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure good point, added the fallback within the method

@themisvaltinos themisvaltinos merged commit 32407f9 into main Sep 9, 2025
36 checks passed
@themisvaltinos themisvaltinos deleted the themis/jinja branch September 9, 2025 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants