From d2f4b6fa3a0eb0a8d40fb47aad71908678402365 Mon Sep 17 00:00:00 2001 From: Jan-Christoph Klie Date: Sun, 27 Jan 2019 23:15:27 +0100 Subject: [PATCH] #11 - Add warning when character was not found (#15) - Display error message on exception --- maobi/quiz.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/maobi/quiz.py b/maobi/quiz.py index 9dc9be6..3092450 100644 --- a/maobi/quiz.py +++ b/maobi/quiz.py @@ -63,7 +63,6 @@ """ ) - class MaobiException(Exception): def __init__(self, message): super(MaobiException, self).__init__(message) @@ -97,7 +96,7 @@ def maobi_hook(html: str, card: Card, context: str) -> str: character_data = _load_character_data(character) except MaobiException as e: debug(maobi_config, str(e)) - return html + return _build_error_message(html, str(e)) # Style the character div depending on the configuration styles = [] @@ -213,3 +212,13 @@ def _build_hanzi_grid_style(grid_type: GridType) -> str: svg_data = f.read() return style.substitute(target_div=TARGET_DIV, svg_data=quote(svg_data)) + + +def _build_error_message(html: str, message: str) -> str: + """ Constructs the HTML for an error text with message `message` over the original html. """ + return f"""

+Maobi encountered an error:
+{message} +

+{html} +"""