Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception causes all over the codebase #5556

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions notebook/bundler/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ def get(self, path):

try:
bundler = self.get_bundler(bundler_id)
except KeyError:
raise web.HTTPError(400, 'Bundler %s not enabled' % bundler_id)

except KeyError as e:
raise web.HTTPError(400, 'Bundler %s not enabled' %
bundler_id) from e

module_name = bundler['module_name']
try:
# no-op in python3, decode error in python2
Expand All @@ -72,8 +73,9 @@ def get(self, path):

try:
bundler_mod = import_item(module_name)
except ImportError:
raise web.HTTPError(500, 'Could not import bundler %s ' % bundler_id)
except ImportError as e:
raise web.HTTPError(500, 'Could not import bundler %s ' %
bundler_id) from e

# Let the bundler respond in any way it sees fit and assume it will
# finish the request
Expand Down
27 changes: 17 additions & 10 deletions notebook/gateway/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,23 @@ def gateway_request(endpoint, **kwargs):
# or the server is not running.
# NOTE: We do this here since this handler is called during the Notebook's startup and subsequent refreshes
# of the tree view.
except ConnectionRefusedError:
raise web.HTTPError(503, "Connection refused from Gateway server url '{}'. "
"Check to be sure the Gateway instance is running.".format(GatewayClient.instance().url))
except ConnectionRefusedError as e:
raise web.HTTPError(
503,
"Connection refused from Gateway server url '{}'. Check to be sure the"
" Gateway instance is running.".format(GatewayClient.instance().url)
) from e
except HTTPError as e:
# This can occur if the host is valid (e.g., foo.com) but there's nothing there.
raise web.HTTPError(e.code, "Error attempting to connect to Gateway server url '{}'. "
"Ensure gateway url is valid and the Gateway instance is running.".
format(GatewayClient.instance().url))
except gaierror:
raise web.HTTPError(404, "The Gateway server specified in the gateway_url '{}' doesn't appear to be valid. "
"Ensure gateway url is valid and the Gateway instance is running.".
format(GatewayClient.instance().url))
format(GatewayClient.instance().url)) from e
except gaierror as e:
raise web.HTTPError(
404,
"The Gateway server specified in the gateway_url '{}' doesn't appear to be valid. Ensure gateway "
"url is valid and the Gateway instance is running.".format(GatewayClient.instance().url)
) from e

raise gen.Return(response)

Expand Down Expand Up @@ -575,8 +580,10 @@ def get_kernel_spec(self, kernel_name, **kwargs):
if error.status_code == 404:
# Convert not found to KeyError since that's what the Notebook handler expects
# message is not used, but might as well make it useful for troubleshooting
raise KeyError('kernelspec {kernel_name} not found on Gateway server at: {gateway_url}'.
format(kernel_name=kernel_name, gateway_url=GatewayClient.instance().url))
raise KeyError(
'kernelspec {kernel_name} not found on Gateway server at: {gateway_url}'.
format(kernel_name=kernel_name, gateway_url=GatewayClient.instance().url)
) from error
else:
raise
else:
Expand Down
5 changes: 3 additions & 2 deletions notebook/kernelspecs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def get(self, kernel_name, path, include_body=True):
ksm = self.kernel_spec_manager
try:
self.root = ksm.get_kernel_spec(kernel_name).resource_dir
except KeyError:
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
except KeyError as e:
raise web.HTTPError(404,
u'Kernel spec %s not found' % kernel_name) from e
self.log.debug("Serving kernel resource from: %s", self.root)
return web.StaticFileHandler.get(self, path, include_body=include_body)

Expand Down
12 changes: 6 additions & 6 deletions notebook/nbconvert/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ def get_exporter(format, **kwargs):
try:
from nbconvert.exporters.base import get_exporter
except ImportError as e:
raise web.HTTPError(500, "Could not import nbconvert: %s" % e)
raise web.HTTPError(500, "Could not import nbconvert: %s" % e) from e

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

try:
return Exporter(**kwargs)
except Exception as e:
app_log.exception("Could not construct Exporter: %s", Exporter)
raise web.HTTPError(500, "Could not construct Exporter: %s" % e)
raise web.HTTPError(500, "Could not construct Exporter: %s" % e) from e

class NbconvertFileHandler(IPythonHandler):

Expand Down Expand Up @@ -133,7 +133,7 @@ def get(self, format, path):
)
except Exception as e:
self.log.exception("nbconvert failed: %s", e)
raise web.HTTPError(500, "nbconvert failed: %s" % e)
raise web.HTTPError(500, "nbconvert failed: %s" % e) from e

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

if respond_zip(self, name, output, resources):
return
Expand Down
16 changes: 8 additions & 8 deletions notebook/notebookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@
# check for tornado 3.1.0
try:
import tornado
except ImportError:
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0"))
except ImportError as e:
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0")) from e
try:
version_info = tornado.version_info
except AttributeError:
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0, but you have < 1.1.0"))
except AttributeError as e:
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0, but you have < 1.1.0")) from e
if version_info < (5,0):
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0, but you have %s") % tornado.version)

Expand Down Expand Up @@ -858,14 +858,14 @@ def _validate_sock_mode(self, proposal):
# And isn't out of bounds.
converted_value <= 2 ** 12
))
except ValueError:
except ValueError as e:
raise TraitError(
'invalid --sock-mode value: %s, please specify as e.g. "0600"' % value
)
except AssertionError:
) from e
except AssertionError as e:
raise TraitError(
'invalid --sock-mode value: %s, must have u+rw (0600) at a minimum' % value
)
) from e
return value


Expand Down
8 changes: 4 additions & 4 deletions notebook/services/contents/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def perm_to_403(self, os_path=''):
if not os_path:
os_path = str_to_unicode(e.filename or 'unknown file')
path = to_api_path(os_path, root=self.root_dir)
raise HTTPError(403, u'Permission denied: %s' % path)
raise HTTPError(403, u'Permission denied: %s' % path) from e
else:
raise

Expand Down Expand Up @@ -310,13 +310,13 @@ def _read_file(self, os_path, format):
# was explicitly requested.
try:
return bcontent.decode('utf8'), 'text'
except UnicodeError:
except UnicodeError as e:
if format == 'text':
raise HTTPError(
400,
"%s is not UTF-8 encoded" % os_path,
reason='bad format',
)
) from e
return encodebytes(bcontent).decode('ascii'), 'base64'

def _save_file(self, os_path, content, format):
Expand All @@ -335,7 +335,7 @@ def _save_file(self, os_path, content, format):
except Exception as e:
raise HTTPError(
400, u'Encoding error saving %s: %s' % (os_path, e)
)
) from e

with self.atomic_writing(os_path, text=False) as f:
f.write(bcontent)
9 changes: 6 additions & 3 deletions notebook/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ def run_post_save_hook(self, model, os_path):
self.post_save_hook(os_path=os_path, model=model, contents_manager=self)
except Exception as e:
self.log.error("Post-save hook failed o-n %s", os_path, exc_info=True)
raise web.HTTPError(500, u'Unexpected error while running post hook save: %s' % e)
raise web.HTTPError(500, u'Unexpected error while running post hook save: %s'
% e) from e

@validate('root_dir')
def _validate_root_dir(self, proposal):
Expand Down Expand Up @@ -487,7 +488,8 @@ def save(self, model, path=''):
raise
except Exception as e:
self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)
raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e))
raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' %
(path, e)) from e

validation_message = None
if model['type'] == 'notebook':
Expand Down Expand Up @@ -584,7 +586,8 @@ def rename_file(self, old_path, new_path):
except web.HTTPError:
raise
except Exception as e:
raise web.HTTPError(500, u'Unknown error renaming file: %s %s' % (old_path, e))
raise web.HTTPError(500, u'Unknown error renaming file: %s %s' %
(old_path, e)) from e

def info_string(self):
return _("Serving notebooks from local directory: %s") % self.root_dir
Expand Down
4 changes: 2 additions & 2 deletions notebook/services/contents/largefilemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def save(self, model, path=''):
raise
except Exception as e:
self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)
raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e))
raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e)) from e

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

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

with self.perm_to_403(os_path):
if os.path.islink(os_path):
Expand Down
4 changes: 2 additions & 2 deletions notebook/services/kernelspecs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def get(self, kernel_name):
kernel_name = url_unescape(kernel_name)
try:
spec = yield maybe_future(ksm.get_kernel_spec(kernel_name))
except KeyError:
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
except KeyError as e:
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name) from e
if is_kernelspec_model(spec):
model = spec
else:
Expand Down
2 changes: 1 addition & 1 deletion notebook/services/nbconvert/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get(self):
try:
from nbconvert.exporters import base
except ImportError as e:
raise web.HTTPError(500, "Could not import nbconvert: %s" % e)
raise web.HTTPError(500, "Could not import nbconvert: %s" % e) from e
res = {}
exporters = base.get_export_names()
for exporter_name in exporters:
Expand Down
12 changes: 6 additions & 6 deletions notebook/services/sessions/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def post(self):

try:
path = model['path']
except KeyError:
raise web.HTTPError(400, "Missing field in JSON data: path")
except KeyError as e:
raise web.HTTPError(400, "Missing field in JSON data: path") from e

try:
mtype = model['type']
except KeyError:
raise web.HTTPError(400, "Missing field in JSON data: type")
except KeyError as e:
raise web.HTTPError(400, "Missing field in JSON data: type") from e

name = model.get('name', None)
kernel = model.get('kernel', {})
Expand Down Expand Up @@ -155,9 +155,9 @@ def delete(self, session_id):
sm = self.session_manager
try:
yield maybe_future(sm.delete_session(session_id))
except KeyError:
except KeyError as e:
# the kernel was deleted but the session wasn't!
raise web.HTTPError(410, "Kernel deleted before session")
raise web.HTTPError(410, "Kernel deleted before session") from e
self.set_status(204)
self.finish()

Expand Down
2 changes: 1 addition & 1 deletion notebook/tests/launchnotebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def wait_until_alive(cls):
cls.fetch_url(url)
except Exception as e:
if not cls.notebook_thread.is_alive():
raise RuntimeError("The notebook server failed to start")
raise RuntimeError("The notebook server failed to start") from e
time.sleep(POLL_INTERVAL)
else:
return
Expand Down
2 changes: 1 addition & 1 deletion notebook/tests/selenium/quick_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def quick_driver(lab=False):
server = list(list_running_servers())[0]
except IndexError as e:
raise NoServerError('You need a server running before you can run '
'this command')
'this command') from e
driver = Firefox()
auth_url = '{url}?token={token}'.format(**server)
driver.get(auth_url)
Expand Down