@@ -117,7 +117,7 @@ def remove_examples(app, exception):
117
117
DESTINATION_DIRECTORY = pathlib .Path (app .srcdir ) / "examples"
118
118
size = directory_size (DESTINATION_DIRECTORY )
119
119
logger .info (f"Removing directory { DESTINATION_DIRECTORY } ({ size } MB)." )
120
- shutil .rmtree (DESTINATION_DIRECTORY )
120
+ shutil .rmtree (DESTINATION_DIRECTORY , ignore_errors = True )
121
121
logger .info (f"Directory removed." )
122
122
123
123
def add_ipython_time (app , docname , source ):
@@ -201,6 +201,22 @@ def remove_ipython_time_from_html(app, pagename, templatename, context, doctree)
201
201
pattern = r'<span class="o">%%time<\/span>\n'
202
202
context ['body' ] = re .sub (pattern , '' , context ['body' ])
203
203
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' )
204
220
205
221
def setup (app ):
206
222
app .add_directive ('pprint' , PrettyPrintDirective )
@@ -209,8 +225,10 @@ def setup(app):
209
225
app .connect ('source-read' , add_ipython_time )
210
226
app .connect ('source-read' , adjust_image_path )
211
227
app .connect ('html-page-context' , remove_ipython_time_from_html )
228
+ app .connect ('html-page-context' , check_example_error )
212
229
app .connect ('build-finished' , remove_examples )
213
230
app .connect ('build-finished' , remove_doctree )
231
+ app .connect ('build-finished' , check_build_finished_without_error )
214
232
215
233
local_path = os .path .dirname (os .path .realpath (__file__ ))
216
234
module_path = pathlib .Path (local_path )
0 commit comments