From ce7bb446d60f22df17c55c4f88cf50f6fba6b44e Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 15 Sep 2023 22:39:35 +0100 Subject: [PATCH] Improve error handling --- hooks/images.py | 2 +- hooks/nav.py | 4 ++++ hooks/pygments-styles.py | 2 +- hooks/static.py | 8 ++++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/hooks/images.py b/hooks/images.py index da51b33..232fb8c 100644 --- a/hooks/images.py +++ b/hooks/images.py @@ -40,7 +40,7 @@ def on_page_content(html, page, config, files: Files): def resize_image(files: Files, image, width): source_file = files.get_file_from_path(image) if source_file is None: - return None + raise ValueError(f"Unable to find file {image!r}") resize = Resize(source_file.abs_src_path, source_file.url, width) RESIZES.add(resize) diff --git a/hooks/nav.py b/hooks/nav.py index 6e2ca28..914e9fb 100644 --- a/hooks/nav.py +++ b/hooks/nav.py @@ -1,5 +1,8 @@ import jinja2 from mkdocs.utils.templates import url_filter +from mkdocs.plugins import get_plugin_logger + +logger = get_plugin_logger("nav") def is_list_page(page) -> bool: if not page.parent: @@ -41,6 +44,7 @@ def get_page(context, slug): for page in nav.pages: if page.file.src_uri == slug: return page + logger.warning("Unable to find page for '%s'", slug) return None @jinja2.pass_context diff --git a/hooks/pygments-styles.py b/hooks/pygments-styles.py index c0f093a..b582013 100644 --- a/hooks/pygments-styles.py +++ b/hooks/pygments-styles.py @@ -16,7 +16,7 @@ on_build_error = on_pre_build = on_post_build def minify_css(styles: str) -> str: wrapped_styles = f"") diff --git a/hooks/static.py b/hooks/static.py index f91a914..68147a7 100644 --- a/hooks/static.py +++ b/hooks/static.py @@ -53,6 +53,10 @@ def make_static_extension(files: Files): def render(self, filename): file = files.get_file_from_path(filename) + + if file is None: + raise ValueError(f"Unable to find static file for {filename!r}") + return f"/{file.url}" return StaticExtension @@ -64,6 +68,10 @@ def make_sri_extension(files: Files): def render(self, filename): file = files.get_file_from_path(filename) + + if file is None: + raise ValueError(f"Unable to find static file for {filename!r}") + with open(file.abs_src_path, "rb") as f: digest = hashlib.file_digest(f, "sha256").hexdigest()