PlaidWeb / Publ

Flexible publishing system for the web
http://publ.beesbuzz.biz/
MIT License
40 stars 4 forks source link

Don't swallow markdown exceptions #491

Open fluffy-critter opened 2 years ago

fluffy-critter commented 2 years ago

Expected Behavior

If there's an exception happening in Publ functionality inside a Jinja template, the exception should bubble up or display in some way rather than being swallowed.

Current Behavior

If an exception is occurring in some contexts, Jinja is happy to just pretend that something is a no-op, which makes it much more difficult to notice and track down issues e.g. #490.

Possible Solution

In bug #490, the following exception was being raised by pygments but swallowed at some level:

DEBUG:publ.markdown:blockcode lang=markdown
DEBUG:publ.markdown:Code: language=markdown args={'max_width': 768, 'footnotes_link': '/2645-Fenced-code-blocks', 'toc_link': '/2645-Fenced-code-blocks', 'code_number_links': '/2645-Fenced-code-blocks'}
Exception ignored from cffi callback <function cb_blockcode at 0x102771870>:
Traceback (most recent call last):
  File "/Users/fluffy/Library/Caches/pypoetry/virtualenvs/publ-XESjGdQG-py3.10/lib/python3.10/site-packages/misaka/callbacks.py", line 15, in cb_blockcode
    result = renderer.blockcode(text, lang)
  File "/Users/fluffy/projects/Publ/publ/markdown.py", line 441, in blockcode
    out += pygments.highlight(str(text), lexer, formatter)
  File "/Users/fluffy/Library/Caches/pypoetry/virtualenvs/publ-XESjGdQG-py3.10/lib/python3.10/site-packages/pygments/__init__.py", line 82, in highlight
    return format(lex(code, lexer), formatter, outfile)
  File "/Users/fluffy/Library/Caches/pypoetry/virtualenvs/publ-XESjGdQG-py3.10/lib/python3.10/site-packages/pygments/__init__.py", line 69, in format
    raise TypeError('format() argument must be a formatter instance, '
TypeError: format() argument must be a formatter instance, not a class

It looks like the exception is being raised from markdown.HtmlRenderer but then cffi is ignoring it for some reason; this is possibly something that can be configured by misaka, but I'm not seeing anything in the docs or source about this.

In the short term the easiest approach is to probably create a function decorator like @render_exceptions and decorate all markdown render methods with that; in the long term, this is yet another reason to migrate away from misaka (#261).

Steps to Reproduce (for bugs)

1. 2. 3. 4.

Context