Skip to content

Syntax error prevention and traceback readability #3

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

Conversation

michalpokusa
Copy link
Contributor

@michalpokusa michalpokusa commented Mar 5, 2024

Overall this PR makes it easier to not make mistakes when writing templates, as it will raise error in more cases instead of silently allowing bad syntax that will result in unexpected rendered string.

💥 Breaking changes:

  • May throw error on templates where previously it silently allowed bad syntax or raised unhandled exceptions e.g.
<!DOCTYPE html>
<html>
    <body>
        {% if context.get("condition") %}{% endif %}

        {% for items in context.get("items") %}
        
        {% endwhile %}
        {% endwhile %}
    </body>
</html>

Now it will expicitly say what is wrong, e.g. SyntaxError: Missing {% while ... %} block for {% endwhile %}

🛠️ Updated/Changed:

  • Default name of template rendering function is now more verbose, thus making traceback more readable
  • Template constructor will raise error if provided template is incorrect e.g. has unclosed if, for or while
  • _create_template_rendering_function automatically adds pass where it might be needed, e.g. when someone puts empty loop where the ending token is right after the starting one

🏗️ Refactor:

  • Some ValueErrors for bad template syntrax are now SyntaxErrors

@michalpokusa michalpokusa force-pushed the syntax-error-prevention-and-traceback-readability branch from 8cb5532 to 369d3b1 Compare March 5, 2024 02:19
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

This looks good to me. Thank you @michalpokusa!

I tested successfully on an ESP32-S3 based device with a modified version of the httpserver_templates.py from HTTPServer repo examples. Tested with both valid and invalid sytnax for:

  • if statement
  • for loop
  • while loop

Would there be any possibility of being able to show the line number of the offending invalid syntax within the error? something like this:

SyntaxError: Missing {% if ... %} block for {% endif %} at line 43

@FoamyGuy FoamyGuy merged commit 1258e2c into adafruit:main Mar 6, 2024
@michalpokusa
Copy link
Contributor Author

@FoamyGuy Thanks for review and merging.

Regarding the exact line, it is a bit tricky considering all the loops, blocks, includes etc.
The way this now works is that all the necessary parts are combined into single string, which is then processed. So even if one is provided with with the exact line, it would be relative to the template after everything got merged.
Also, one can have a template in string that references other files.

I do not really have a good idea how this could work in order to be precise about exact place where error occured.

I will try to tinker with it this week, maybe I will come up with something.

@michalpokusa
Copy link
Contributor Author

michalpokusa commented Mar 7, 2024

In theory, I could not modify the template itself, but keep track of processed end position, and print a fragment like this that would help locate the exact token that caused the error:
image

EDIT1:
After some more work I managed to somewhat recreate how Python itself points to incorrect code. I believe that could be a acceptable middle ground.
image

adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Mar 7, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_TemplateEngine to 1.2.0 from 1.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_TemplateEngine#3 from michalpokusa/syntax-error-prevention-and-traceback-readability

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
@michalpokusa michalpokusa deleted the syntax-error-prevention-and-traceback-readability branch March 7, 2024 23:11
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