Skip to content
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

Track Current Processed Node In The Context #760

Merged
merged 3 commits into from
Oct 11, 2021
Merged

Track Current Processed Node In The Context #760

merged 3 commits into from
Oct 11, 2021

Conversation

bhreinb
Copy link
Contributor

@bhreinb bhreinb commented Oct 4, 2021

Hi there,

I'd like to provide more detail when an exception is fired as a result of processing a macro block. The use case is something like the following:

I have a main template file that has the following snippet:

     {% import 'com/<companyName>/macros/translate.jinja' as macro %}

     {{macro.translateWord("hello", "de")}}  
     {{macro.translateWord("world", "es")}}

the macro has something like the following:

{%- macro translateWord(word, languageIso) %}
       {% set supportedIsoLanguages = ["en", "de", "fr"] %}
       {{ assert:contains(languageIso, supportedIsoLanguages) }}
       {{ util:translateString(word, languageIso) }}
{% endmacro -%}

So the first item renders as expected...in the second case it fails because it is not a supported language because of the assert:contains...however the error I’d like to report back is something like the following:

Expression `{{macro.translateWord("world", "es")}}` found at line 4 
failed because of expression `{{ assert:contains(languageIso, supportedLanguages) }}` at line 2.

I can get the current line via the interpreter (that would give me line 2 of the macro function) but I’d like to include more detail namely the original expression that dispatched to the macro function if at all possible. This is particularly useful if macro.translateWord is initiated often for example.

Copy link
Contributor

@jasmith-hs jasmith-hs left a comment

Choose a reason for hiding this comment

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

Could you add a description to this PR that describes basically what you're wanting this for. (You are using jinjava and want to better track errors that result from macro function usage)

I don't think there are really any downsides to adding this. It is a very lightweight operation that provides more visibility about the interpreting process.

Do you have any opinions @boulter

src/main/java/com/hubspot/jinjava/interpret/Context.java Outdated Show resolved Hide resolved
src/main/java/com/hubspot/jinjava/interpret/Context.java Outdated Show resolved Hide resolved
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.

2 participants