@@ -117,7 +117,7 @@ def remove_examples(app, exception):
117117 DESTINATION_DIRECTORY = pathlib .Path (app .srcdir ) / "examples"
118118 size = directory_size (DESTINATION_DIRECTORY )
119119 logger .info (f"Removing directory { DESTINATION_DIRECTORY } ({ size } MB)." )
120- shutil .rmtree (DESTINATION_DIRECTORY )
120+ shutil .rmtree (DESTINATION_DIRECTORY , ignore_errors = True )
121121 logger .info (f"Directory removed." )
122122
123123def add_ipython_time (app , docname , source ):
@@ -201,6 +201,22 @@ def remove_ipython_time_from_html(app, pagename, templatename, context, doctree)
201201 pattern = r'<span class="o">%%time<\/span>\n'
202202 context ['body' ] = re .sub (pattern , '' , context ['body' ])
203203
204+ def check_example_error (app , pagename , templatename , context , doctree ):
205+ """Log an error if the execution of an example as a notebook triggered an error.
206+
207+ Since the documentation build might not stop if the execution of a notebook triggered
208+ an error, we use a flag to log that an error is spotted in the html page context.
209+ """
210+ # Check if the HTML contains an error message
211+ if pagename .startswith ("examples" ) and not pagename .endswith ("/index" ):
212+ if any (map (lambda msg : msg in context ['body' ], ['UsageError' , 'NameError' ])):
213+ logger .error (f"An error was detected in file { pagename } " )
214+ app .builder .config .html_context ['build_error' ] = True
215+
216+ def check_build_finished_without_error (app , exception ):
217+ """Check that no error is detected along the documentation build process."""
218+ if app .builder .config .html_context .get ('build_error' , False ):
219+ raise Exception ('Build failed due to error in html-page-context' )
204220
205221def setup (app ):
206222 app .add_directive ('pprint' , PrettyPrintDirective )
@@ -209,8 +225,10 @@ def setup(app):
209225 app .connect ('source-read' , add_ipython_time )
210226 app .connect ('source-read' , adjust_image_path )
211227 app .connect ('html-page-context' , remove_ipython_time_from_html )
228+ app .connect ('html-page-context' , check_example_error )
212229 app .connect ('build-finished' , remove_examples )
213230 app .connect ('build-finished' , remove_doctree )
231+ app .connect ('build-finished' , check_build_finished_without_error )
214232
215233local_path = os .path .dirname (os .path .realpath (__file__ ))
216234module_path = pathlib .Path (local_path )
0 commit comments