handle graphviz parsing errors more cleanly

This commit is contained in:
Brian S. Stephan 2021-06-24 11:37:57 -05:00
parent 41a53a2a13
commit e61c55bed2
4 changed files with 30 additions and 0 deletions

View File

@ -1,10 +1,13 @@
"""Serve dot diagrams inline."""
import base64
import logging
import re
import markdown
import pydot
logger = logging.getLogger(__name__)
class InlinePydot(markdown.Extension):
"""Wrap the markdown prepcoressor."""
@ -29,6 +32,9 @@ class InlinePydotPreprocessor(markdown.preprocessors.Preprocessor):
# use pydot to turn the text into pydot
graphs = pydot.graph_from_dot_data(dot_string)
if not graphs:
logger.debug("some kind of issue with parsed 'dot' %s", dot_string)
raise ValueError("error parsing dot text!")
# encode the image and provide as an inline image in markdown
encoded_image = base64.b64encode(graphs[0].create_png()).decode('ascii')

View File

@ -61,6 +61,9 @@ def handle_markdown_file_path(resolved_path):
try:
md = init_md()
content = Markup(md.convert(entry))
except ValueError:
logger.exception("error parsing/rendering markdown!")
abort(500)
except TypeError:
logger.exception("error loading/rendering markdown!")
abort(500)

View File

@ -27,3 +27,13 @@ def test_graphviz_is_rendered():
assert response.status_code == 200
assert b'~~~pydot' not in response.data
assert b'data:image/png;base64' in response.data
def test_invalid_graphviz_is_not_rendered():
"""Check that invalid graphviz doesn't blow things up."""
app = app_with_pydot()
client = app.test_client()
response = client.get('/test-invalid-graphviz')
assert response.status_code == 500
assert b'INTERNAL SERVER ERROR' in response.data

View File

@ -0,0 +1,11 @@
# test
test
~~~pydot:attack-plan
rankdir=LR
Earth
Mars
Earth -> Mars
}
~~~
more test