sqlalchemy/mako

`SyntaxException` messages are unhelpful in some cases.

Asday opened this issue · 4 comments

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.

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.

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.

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

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