Skip to content

Commit 7d37528

Browse files
committed
Fix exception causes all over the codebase
1 parent db46c59 commit 7d37528

File tree

13 files changed

+64
-51
lines changed

13 files changed

+64
-51
lines changed

notebook/bundler/handlers.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ def get(self, path):
5959

6060
try:
6161
bundler = self.get_bundler(bundler_id)
62-
except KeyError:
63-
raise web.HTTPError(400, 'Bundler %s not enabled' % bundler_id)
64-
62+
except KeyError as e:
63+
raise web.HTTPError(400, 'Bundler %s not enabled' %
64+
bundler_id) from e
65+
6566
module_name = bundler['module_name']
6667
try:
6768
# no-op in python3, decode error in python2
@@ -72,8 +73,9 @@ def get(self, path):
7273

7374
try:
7475
bundler_mod = import_item(module_name)
75-
except ImportError:
76-
raise web.HTTPError(500, 'Could not import bundler %s ' % bundler_id)
76+
except ImportError as e:
77+
raise web.HTTPError(500, 'Could not import bundler %s ' %
78+
bundler_id) from e
7779

7880
# Let the bundler respond in any way it sees fit and assume it will
7981
# finish the request

notebook/gateway/managers.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,18 +282,23 @@ def gateway_request(endpoint, **kwargs):
282282
# or the server is not running.
283283
# NOTE: We do this here since this handler is called during the Notebook's startup and subsequent refreshes
284284
# of the tree view.
285-
except ConnectionRefusedError:
286-
raise web.HTTPError(503, "Connection refused from Gateway server url '{}'. "
287-
"Check to be sure the Gateway instance is running.".format(GatewayClient.instance().url))
285+
except ConnectionRefusedError as e:
286+
raise web.HTTPError(
287+
503,
288+
"Connection refused from Gateway server url '{}'. Check to be sure the"
289+
" Gateway instance is running.".format(GatewayClient.instance().url)
290+
) from e
288291
except HTTPError as e:
289292
# This can occur if the host is valid (e.g., foo.com) but there's nothing there.
290293
raise web.HTTPError(e.code, "Error attempting to connect to Gateway server url '{}'. "
291294
"Ensure gateway url is valid and the Gateway instance is running.".
292-
format(GatewayClient.instance().url))
293-
except gaierror:
294-
raise web.HTTPError(404, "The Gateway server specified in the gateway_url '{}' doesn't appear to be valid. "
295-
"Ensure gateway url is valid and the Gateway instance is running.".
296-
format(GatewayClient.instance().url))
295+
format(GatewayClient.instance().url)) from e
296+
except gaierror as e:
297+
raise web.HTTPError(
298+
404,
299+
"The Gateway server specified in the gateway_url '{}' doesn't appear to be valid. Ensure gateway "
300+
"url is valid and the Gateway instance is running.".format(GatewayClient.instance().url)
301+
) from e
297302

298303
raise gen.Return(response)
299304

@@ -575,8 +580,10 @@ def get_kernel_spec(self, kernel_name, **kwargs):
575580
if error.status_code == 404:
576581
# Convert not found to KeyError since that's what the Notebook handler expects
577582
# message is not used, but might as well make it useful for troubleshooting
578-
raise KeyError('kernelspec {kernel_name} not found on Gateway server at: {gateway_url}'.
579-
format(kernel_name=kernel_name, gateway_url=GatewayClient.instance().url))
583+
raise KeyError(
584+
'kernelspec {kernel_name} not found on Gateway server at: {gateway_url}'.
585+
format(kernel_name=kernel_name, gateway_url=GatewayClient.instance().url)
586+
) from error
580587
else:
581588
raise
582589
else:

notebook/kernelspecs/handlers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ def get(self, kernel_name, path, include_body=True):
1313
ksm = self.kernel_spec_manager
1414
try:
1515
self.root = ksm.get_kernel_spec(kernel_name).resource_dir
16-
except KeyError:
17-
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
16+
except KeyError as e:
17+
raise web.HTTPError(404,
18+
u'Kernel spec %s not found' % kernel_name) from e
1819
self.log.debug("Serving kernel resource from: %s", self.root)
1920
return web.StaticFileHandler.get(self, path, include_body=include_body)
2021

notebook/nbconvert/handlers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ def get_exporter(format, **kwargs):
6161
try:
6262
from nbconvert.exporters.base import get_exporter
6363
except ImportError as e:
64-
raise web.HTTPError(500, "Could not import nbconvert: %s" % e)
64+
raise web.HTTPError(500, "Could not import nbconvert: %s" % e) from e
6565

6666
try:
6767
Exporter = get_exporter(format)
68-
except KeyError:
68+
except KeyError as e:
6969
# should this be 400?
70-
raise web.HTTPError(404, u"No exporter for format: %s" % format)
70+
raise web.HTTPError(404, u"No exporter for format: %s" % format) from e
7171

7272
try:
7373
return Exporter(**kwargs)
7474
except Exception as e:
7575
app_log.exception("Could not construct Exporter: %s", Exporter)
76-
raise web.HTTPError(500, "Could not construct Exporter: %s" % e)
76+
raise web.HTTPError(500, "Could not construct Exporter: %s" % e) from e
7777

7878
class NbconvertFileHandler(IPythonHandler):
7979

@@ -133,7 +133,7 @@ def get(self, format, path):
133133
)
134134
except Exception as e:
135135
self.log.exception("nbconvert failed: %s", e)
136-
raise web.HTTPError(500, "nbconvert failed: %s" % e)
136+
raise web.HTTPError(500, "nbconvert failed: %s" % e) from e
137137

138138
if respond_zip(self, name, output, resources):
139139
return
@@ -175,7 +175,7 @@ def post(self, format):
175175
"config_dir": self.application.settings['config_dir'],
176176
})
177177
except Exception as e:
178-
raise web.HTTPError(500, "nbconvert failed: %s" % e)
178+
raise web.HTTPError(500, "nbconvert failed: %s" % e) from e
179179

180180
if respond_zip(self, name, output, resources):
181181
return

notebook/notebookapp.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@
5454
# check for tornado 3.1.0
5555
try:
5656
import tornado
57-
except ImportError:
58-
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0"))
57+
except ImportError as e:
58+
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0")) from e
5959
try:
6060
version_info = tornado.version_info
61-
except AttributeError:
62-
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0, but you have < 1.1.0"))
61+
except AttributeError as e:
62+
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0, but you have < 1.1.0")) from e
6363
if version_info < (5,0):
6464
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0, but you have %s") % tornado.version)
6565

@@ -858,14 +858,14 @@ def _validate_sock_mode(self, proposal):
858858
# And isn't out of bounds.
859859
converted_value <= 2 ** 12
860860
))
861-
except ValueError:
861+
except ValueError as e:
862862
raise TraitError(
863863
'invalid --sock-mode value: %s, please specify as e.g. "0600"' % value
864-
)
865-
except AssertionError:
864+
) from e
865+
except AssertionError as e:
866866
raise TraitError(
867867
'invalid --sock-mode value: %s, must have u+rw (0600) at a minimum' % value
868-
)
868+
) from e
869869
return value
870870

871871

notebook/services/contents/fileio.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def perm_to_403(self, os_path=''):
226226
if not os_path:
227227
os_path = str_to_unicode(e.filename or 'unknown file')
228228
path = to_api_path(os_path, root=self.root_dir)
229-
raise HTTPError(403, u'Permission denied: %s' % path)
229+
raise HTTPError(403, u'Permission denied: %s' % path) from e
230230
else:
231231
raise
232232

@@ -310,13 +310,13 @@ def _read_file(self, os_path, format):
310310
# was explicitly requested.
311311
try:
312312
return bcontent.decode('utf8'), 'text'
313-
except UnicodeError:
313+
except UnicodeError as e:
314314
if format == 'text':
315315
raise HTTPError(
316316
400,
317317
"%s is not UTF-8 encoded" % os_path,
318318
reason='bad format',
319-
)
319+
) from e
320320
return encodebytes(bcontent).decode('ascii'), 'base64'
321321

322322
def _save_file(self, os_path, content, format):
@@ -335,7 +335,7 @@ def _save_file(self, os_path, content, format):
335335
except Exception as e:
336336
raise HTTPError(
337337
400, u'Encoding error saving %s: %s' % (os_path, e)
338-
)
338+
) from e
339339

340340
with self.atomic_writing(os_path, text=False) as f:
341341
f.write(bcontent)

notebook/services/contents/filemanager.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ def run_post_save_hook(self, model, os_path):
131131
self.post_save_hook(os_path=os_path, model=model, contents_manager=self)
132132
except Exception as e:
133133
self.log.error("Post-save hook failed o-n %s", os_path, exc_info=True)
134-
raise web.HTTPError(500, u'Unexpected error while running post hook save: %s' % e)
134+
raise web.HTTPError(500, u'Unexpected error while running post hook save: %s'
135+
% e) from e
135136

136137
@validate('root_dir')
137138
def _validate_root_dir(self, proposal):
@@ -487,7 +488,8 @@ def save(self, model, path=''):
487488
raise
488489
except Exception as e:
489490
self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)
490-
raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e))
491+
raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' %
492+
(path, e)) from e
491493

492494
validation_message = None
493495
if model['type'] == 'notebook':
@@ -584,7 +586,8 @@ def rename_file(self, old_path, new_path):
584586
except web.HTTPError:
585587
raise
586588
except Exception as e:
587-
raise web.HTTPError(500, u'Unknown error renaming file: %s %s' % (old_path, e))
589+
raise web.HTTPError(500, u'Unknown error renaming file: %s %s' %
590+
(old_path, e)) from e
588591

589592
def info_string(self):
590593
return _("Serving notebooks from local directory: %s") % self.root_dir

notebook/services/contents/largefilemanager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def save(self, model, path=''):
3434
raise
3535
except Exception as e:
3636
self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)
37-
raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e))
37+
raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e)) from e
3838

3939
model = self.get(path, content=False)
4040

@@ -61,7 +61,7 @@ def _save_large_file(self, os_path, content, format):
6161
except Exception as e:
6262
raise web.HTTPError(
6363
400, u'Encoding error saving %s: %s' % (os_path, e)
64-
)
64+
) from e
6565

6666
with self.perm_to_403(os_path):
6767
if os.path.islink(os_path):

notebook/services/kernelspecs/handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def get(self, kernel_name):
8787
kernel_name = url_unescape(kernel_name)
8888
try:
8989
spec = yield maybe_future(ksm.get_kernel_spec(kernel_name))
90-
except KeyError:
91-
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
90+
except KeyError as e:
91+
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name) from e
9292
if is_kernelspec_model(spec):
9393
model = spec
9494
else:

notebook/services/nbconvert/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def get(self):
1313
try:
1414
from nbconvert.exporters import base
1515
except ImportError as e:
16-
raise web.HTTPError(500, "Could not import nbconvert: %s" % e)
16+
raise web.HTTPError(500, "Could not import nbconvert: %s" % e) from e
1717
res = {}
1818
exporters = base.get_export_names()
1919
for exporter_name in exporters:

0 commit comments

Comments
 (0)