From e61c55bed251cc8f15048630e56affff70661ed2 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 24 Jun 2021 11:37:57 -0500 Subject: [PATCH] handle graphviz parsing errors more cleanly --- incorporealcms/mdx/pydot.py | 6 ++++++ incorporealcms/pages.py | 3 +++ tests/functional_markdown_tests.py | 10 ++++++++++ tests/instance/pages/test-invalid-graphviz.md | 11 +++++++++++ 4 files changed, 30 insertions(+) create mode 100644 tests/instance/pages/test-invalid-graphviz.md diff --git a/incorporealcms/mdx/pydot.py b/incorporealcms/mdx/pydot.py index 9161624..e644a58 100644 --- a/incorporealcms/mdx/pydot.py +++ b/incorporealcms/mdx/pydot.py @@ -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') diff --git a/incorporealcms/pages.py b/incorporealcms/pages.py index a2b034e..1fc2f61 100644 --- a/incorporealcms/pages.py +++ b/incorporealcms/pages.py @@ -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) diff --git a/tests/functional_markdown_tests.py b/tests/functional_markdown_tests.py index 8125e98..2b7a65b 100644 --- a/tests/functional_markdown_tests.py +++ b/tests/functional_markdown_tests.py @@ -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 diff --git a/tests/instance/pages/test-invalid-graphviz.md b/tests/instance/pages/test-invalid-graphviz.md new file mode 100644 index 0000000..27727f8 --- /dev/null +++ b/tests/instance/pages/test-invalid-graphviz.md @@ -0,0 +1,11 @@ +# test + +test +~~~pydot:attack-plan + rankdir=LR + Earth + Mars + Earth -> Mars +} +~~~ +more test