From 88ee3d35d816af99a853a0b348f5d0a9fe97f891 Mon Sep 17 00:00:00 2001 From: Cory Gwin Date: Fri, 10 Dec 2021 13:15:01 -0500 Subject: [PATCH] Add an invalid notebook error (#1675) * Add an invalid notebook error Motivation: - As we are now service hundreds of thousands of notebooks a day, we are seeing a number of errors from Notebook rendering that really stem from notebooks not being properly formated. These show up in a number of errors such as the error handled in this pr, an assertion error, we have also seen key errors and others. This pr is a bit of a proposal to instead introduced a common exception that indicates the notebook is missing some required data that is needed to render itself. * Update nbconvert/filters/markdown_mistune.py Co-authored-by: Matthias Bussonnier Co-authored-by: Matthias Bussonnier --- nbconvert/filters/markdown_mistune.py | 5 ++++- nbconvert/nbconvertapp.py | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/nbconvert/filters/markdown_mistune.py b/nbconvert/filters/markdown_mistune.py index ff209f372..a46be513f 100644 --- a/nbconvert/filters/markdown_mistune.py +++ b/nbconvert/filters/markdown_mistune.py @@ -160,7 +160,10 @@ def image(self, src, title, text): attachment_prefix = 'attachment:' if src.startswith(attachment_prefix): name = src[len(attachment_prefix):] - assert name in attachments, "missing attachment: {}".format(name) + + if not name in attachments: + raise InvalidNotebook("missing attachment: {}".format(name)) + attachment = attachments[name] # we choose vector over raster, and lossless over lossy preferred_mime_types = ['image/svg+xml', 'image/png', 'image/jpeg'] diff --git a/nbconvert/nbconvertapp.py b/nbconvert/nbconvertapp.py index e137a173d..97ca798b2 100755 --- a/nbconvert/nbconvertapp.py +++ b/nbconvert/nbconvertapp.py @@ -595,6 +595,9 @@ def initialize(self, argv=None): def default_export_format(self): return 'html' +class InvalidNotebook(Exception): + pass + #----------------------------------------------------------------------------- # Main entry point #-----------------------------------------------------------------------------