sqlalchemy / mako

Mako Templates for Python
https://www.makotemplates.org
MIT License
353 stars 60 forks source link

`SyntaxException` messages are unhelpful in some cases. #407

Closed Asday closed 1 month ago

Asday commented 1 month ago

I am new to Mako templates, and making the usual rookie errors.

I'm coming from Django templates, where {% extends "base.html" %} and {% block blockname %} require no extra decoration, so I have several times done <% inherit "base.html" /> and <%block "content" />, both of which appear to pick an arbitrary = hundreds of lines of template code later and claim the problem is there, e.g.:

mako-issue ⚡ python test.py
Traceback (most recent call last):
  File ".../python3.10/site-packages/mako/lookup.py", line 240, in get_template
    return self._check(uri, self._collection[uri])
KeyError: 'child_template.html'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/asday/src/github.com/asday/mako-issue/test.py", line 105, in test
    html_template = lookup.get_template(
  File ".../python3.10/site-packages/mako/lookup.py", line 251, in get_template
    return self._load(srcfile, uri)
  File ".../python3.10/site-packages/mako/lookup.py", line 312, in _load
    self._collection[uri] = template = Template(
  File ".../python3.10/site-packages/mako/template.py", line 316, in __init__
    module = self._compile_from_file(path, filename)
  File ".../python3.10/site-packages/mako/template.py", line 392, in _compile_from_file
    code, module = _compile_text(self, data, filename)
  File ".../python3.10/site-packages/mako/template.py", line 676, in _compile_text
    source, lexer = _compile(
  File ".../python3.10/site-packages/mako/template.py", line 656, in _compile
    node = lexer.parse()
  File ".../python3.10/site-packages/mako/lexer.py", line 248, in parse
    if self.match_python_block():
  File ".../python3.10/site-packages/mako/lexer.py", line 400, in match_python_block
    text, end = self.parse_until_text(False, r"%>")
  File ".../python3.10/site-packages/mako/lexer.py", line 120, in parse_until_text
    raise exceptions.SyntaxException(
mako.exceptions.SyntaxException: Expected: %> in file '/home/asday/src/github.com/asday/mako-issue/child_template.html' at line: 46 char: 16

This is, in my opinion, an incorrect error message. That is explaining why the state machine failed to halt successfully, but that's not useful to the user, who instead cares why the state machine is in that state in the first place.

I'm not able to suggest fixes without spending a lot more time on this, but something like "expected %> in file ... at ..., while parsing <%inherit at ..." would be good, then I'd at least have know which tags were causing the issue rather than having to binary search delete chunks of my file to figure out where the issue really is.

zzzeek commented 1 month ago

well it's not "incorrect" it's just not providing context that's as helpful as that of other parsers.

Mako is very much a legacy library and this parser was ported from another parser written in Perl about 25 years ago at this point, and as such it has limited ability to provide better information than this. If you're starting a new project I would recommend a much more mature template language like Jinja.

Asday commented 1 month ago

Unforch. I'm not starting a new project, rather a new job, so I don't get to make those decisions.

Thank you for your time.

zzzeek commented 1 month ago

I'm not opposed to improvements it's just I dont personally have resources to rearchitect Mako's parser here.

Asday commented 1 month ago

Yeah no 100% I get you, the "unforch" was a "woe is me", and the "thank you for your time" was genuine.