Merge pull request #5556 from cool-RR/2020-06-25-raise-from

Fix exception causes all over the codebase
This commit is contained in:
Kevin Bates 2020-07-01 08:41:52 -07:00 committed by GitHub
commit 94af525a2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 64 additions and 51 deletions

View File

@ -59,9 +59,10 @@ class BundlerHandler(IPythonHandler):
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
@ -72,8 +73,9 @@ class BundlerHandler(IPythonHandler):
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

View File

@ -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)
@ -575,8 +580,10 @@ class GatewayKernelSpecManager(KernelSpecManager):
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:

View File

@ -13,8 +13,9 @@ class KernelSpecResourceHandler(web.StaticFileHandler, IPythonHandler):
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)

View File

@ -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):
@ -133,7 +133,7 @@ class NbconvertFileHandler(IPythonHandler):
)
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
@ -175,7 +175,7 @@ class NbconvertPostHandler(IPythonHandler):
"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

View File

@ -54,12 +54,12 @@ ioloop.install()
# 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)
@ -858,14 +858,14 @@ class NotebookApp(JupyterApp):
# 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

View File

@ -226,7 +226,7 @@ class FileManagerMixin(Configurable):
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
@ -310,13 +310,13 @@ class FileManagerMixin(Configurable):
# 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):
@ -335,7 +335,7 @@ class FileManagerMixin(Configurable):
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)

View File

@ -131,7 +131,8 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
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):
@ -487,7 +488,8 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
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':
@ -584,7 +586,8 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
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

View File

@ -34,7 +34,7 @@ class LargeFileManager(FileContentsManager):
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)
@ -61,7 +61,7 @@ class LargeFileManager(FileContentsManager):
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):

View File

@ -87,8 +87,8 @@ class KernelSpecHandler(APIHandler):
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:

View File

@ -13,7 +13,7 @@ class NbconvertRootHandler(APIHandler):
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:

View File

@ -44,13 +44,13 @@ class SessionRootHandler(APIHandler):
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', {})
@ -155,9 +155,9 @@ class SessionHandler(APIHandler):
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()

View File

@ -56,7 +56,7 @@ class NotebookTestBase(TestCase):
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

View File

@ -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)