mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-24 12:05:22 +08:00
Fix exception causes all over the codebase
This commit is contained in:
parent
db46c594bb
commit
7d375282a9
@ -59,9 +59,10 @@ class BundlerHandler(IPythonHandler):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
bundler = self.get_bundler(bundler_id)
|
bundler = self.get_bundler(bundler_id)
|
||||||
except KeyError:
|
except KeyError as e:
|
||||||
raise web.HTTPError(400, 'Bundler %s not enabled' % bundler_id)
|
raise web.HTTPError(400, 'Bundler %s not enabled' %
|
||||||
|
bundler_id) from e
|
||||||
|
|
||||||
module_name = bundler['module_name']
|
module_name = bundler['module_name']
|
||||||
try:
|
try:
|
||||||
# no-op in python3, decode error in python2
|
# no-op in python3, decode error in python2
|
||||||
@ -72,8 +73,9 @@ class BundlerHandler(IPythonHandler):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
bundler_mod = import_item(module_name)
|
bundler_mod = import_item(module_name)
|
||||||
except ImportError:
|
except ImportError as e:
|
||||||
raise web.HTTPError(500, 'Could not import bundler %s ' % bundler_id)
|
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
|
# Let the bundler respond in any way it sees fit and assume it will
|
||||||
# finish the request
|
# finish the request
|
||||||
|
@ -282,18 +282,23 @@ def gateway_request(endpoint, **kwargs):
|
|||||||
# or the server is not running.
|
# or the server is not running.
|
||||||
# NOTE: We do this here since this handler is called during the Notebook's startup and subsequent refreshes
|
# NOTE: We do this here since this handler is called during the Notebook's startup and subsequent refreshes
|
||||||
# of the tree view.
|
# of the tree view.
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError as e:
|
||||||
raise web.HTTPError(503, "Connection refused from Gateway server url '{}'. "
|
raise web.HTTPError(
|
||||||
"Check to be sure the Gateway instance is running.".format(GatewayClient.instance().url))
|
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:
|
except HTTPError as e:
|
||||||
# This can occur if the host is valid (e.g., foo.com) but there's nothing there.
|
# 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 '{}'. "
|
raise web.HTTPError(e.code, "Error attempting to connect to Gateway server url '{}'. "
|
||||||
"Ensure gateway url is valid and the Gateway instance is running.".
|
"Ensure gateway url is valid and the Gateway instance is running.".
|
||||||
format(GatewayClient.instance().url))
|
format(GatewayClient.instance().url)) from e
|
||||||
except gaierror:
|
except gaierror as e:
|
||||||
raise web.HTTPError(404, "The Gateway server specified in the gateway_url '{}' doesn't appear to be valid. "
|
raise web.HTTPError(
|
||||||
"Ensure gateway url is valid and the Gateway instance is running.".
|
404,
|
||||||
format(GatewayClient.instance().url))
|
"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)
|
raise gen.Return(response)
|
||||||
|
|
||||||
@ -575,8 +580,10 @@ class GatewayKernelSpecManager(KernelSpecManager):
|
|||||||
if error.status_code == 404:
|
if error.status_code == 404:
|
||||||
# Convert not found to KeyError since that's what the Notebook handler expects
|
# 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
|
# 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}'.
|
raise KeyError(
|
||||||
format(kernel_name=kernel_name, gateway_url=GatewayClient.instance().url))
|
'kernelspec {kernel_name} not found on Gateway server at: {gateway_url}'.
|
||||||
|
format(kernel_name=kernel_name, gateway_url=GatewayClient.instance().url)
|
||||||
|
) from error
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
|
@ -13,8 +13,9 @@ class KernelSpecResourceHandler(web.StaticFileHandler, IPythonHandler):
|
|||||||
ksm = self.kernel_spec_manager
|
ksm = self.kernel_spec_manager
|
||||||
try:
|
try:
|
||||||
self.root = ksm.get_kernel_spec(kernel_name).resource_dir
|
self.root = ksm.get_kernel_spec(kernel_name).resource_dir
|
||||||
except KeyError:
|
except KeyError as e:
|
||||||
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
|
raise web.HTTPError(404,
|
||||||
|
u'Kernel spec %s not found' % kernel_name) from e
|
||||||
self.log.debug("Serving kernel resource from: %s", self.root)
|
self.log.debug("Serving kernel resource from: %s", self.root)
|
||||||
return web.StaticFileHandler.get(self, path, include_body=include_body)
|
return web.StaticFileHandler.get(self, path, include_body=include_body)
|
||||||
|
|
||||||
|
@ -61,19 +61,19 @@ def get_exporter(format, **kwargs):
|
|||||||
try:
|
try:
|
||||||
from nbconvert.exporters.base import get_exporter
|
from nbconvert.exporters.base import get_exporter
|
||||||
except ImportError as e:
|
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:
|
try:
|
||||||
Exporter = get_exporter(format)
|
Exporter = get_exporter(format)
|
||||||
except KeyError:
|
except KeyError as e:
|
||||||
# should this be 400?
|
# 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:
|
try:
|
||||||
return Exporter(**kwargs)
|
return Exporter(**kwargs)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
app_log.exception("Could not construct Exporter: %s", Exporter)
|
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):
|
class NbconvertFileHandler(IPythonHandler):
|
||||||
|
|
||||||
@ -133,7 +133,7 @@ class NbconvertFileHandler(IPythonHandler):
|
|||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log.exception("nbconvert failed: %s", 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):
|
if respond_zip(self, name, output, resources):
|
||||||
return
|
return
|
||||||
@ -175,7 +175,7 @@ class NbconvertPostHandler(IPythonHandler):
|
|||||||
"config_dir": self.application.settings['config_dir'],
|
"config_dir": self.application.settings['config_dir'],
|
||||||
})
|
})
|
||||||
except Exception as e:
|
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):
|
if respond_zip(self, name, output, resources):
|
||||||
return
|
return
|
||||||
|
@ -54,12 +54,12 @@ ioloop.install()
|
|||||||
# check for tornado 3.1.0
|
# check for tornado 3.1.0
|
||||||
try:
|
try:
|
||||||
import tornado
|
import tornado
|
||||||
except ImportError:
|
except ImportError as e:
|
||||||
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0"))
|
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0")) from e
|
||||||
try:
|
try:
|
||||||
version_info = tornado.version_info
|
version_info = tornado.version_info
|
||||||
except AttributeError:
|
except AttributeError as e:
|
||||||
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0, but you have < 1.1.0"))
|
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0, but you have < 1.1.0")) from e
|
||||||
if version_info < (5,0):
|
if version_info < (5,0):
|
||||||
raise ImportError(_("The Jupyter Notebook requires tornado >= 5.0, but you have %s") % tornado.version)
|
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.
|
# And isn't out of bounds.
|
||||||
converted_value <= 2 ** 12
|
converted_value <= 2 ** 12
|
||||||
))
|
))
|
||||||
except ValueError:
|
except ValueError as e:
|
||||||
raise TraitError(
|
raise TraitError(
|
||||||
'invalid --sock-mode value: %s, please specify as e.g. "0600"' % value
|
'invalid --sock-mode value: %s, please specify as e.g. "0600"' % value
|
||||||
)
|
) from e
|
||||||
except AssertionError:
|
except AssertionError as e:
|
||||||
raise TraitError(
|
raise TraitError(
|
||||||
'invalid --sock-mode value: %s, must have u+rw (0600) at a minimum' % value
|
'invalid --sock-mode value: %s, must have u+rw (0600) at a minimum' % value
|
||||||
)
|
) from e
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
@ -226,7 +226,7 @@ class FileManagerMixin(Configurable):
|
|||||||
if not os_path:
|
if not os_path:
|
||||||
os_path = str_to_unicode(e.filename or 'unknown file')
|
os_path = str_to_unicode(e.filename or 'unknown file')
|
||||||
path = to_api_path(os_path, root=self.root_dir)
|
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:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
@ -310,13 +310,13 @@ class FileManagerMixin(Configurable):
|
|||||||
# was explicitly requested.
|
# was explicitly requested.
|
||||||
try:
|
try:
|
||||||
return bcontent.decode('utf8'), 'text'
|
return bcontent.decode('utf8'), 'text'
|
||||||
except UnicodeError:
|
except UnicodeError as e:
|
||||||
if format == 'text':
|
if format == 'text':
|
||||||
raise HTTPError(
|
raise HTTPError(
|
||||||
400,
|
400,
|
||||||
"%s is not UTF-8 encoded" % os_path,
|
"%s is not UTF-8 encoded" % os_path,
|
||||||
reason='bad format',
|
reason='bad format',
|
||||||
)
|
) from e
|
||||||
return encodebytes(bcontent).decode('ascii'), 'base64'
|
return encodebytes(bcontent).decode('ascii'), 'base64'
|
||||||
|
|
||||||
def _save_file(self, os_path, content, format):
|
def _save_file(self, os_path, content, format):
|
||||||
@ -335,7 +335,7 @@ class FileManagerMixin(Configurable):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPError(
|
raise HTTPError(
|
||||||
400, u'Encoding error saving %s: %s' % (os_path, e)
|
400, u'Encoding error saving %s: %s' % (os_path, e)
|
||||||
)
|
) from e
|
||||||
|
|
||||||
with self.atomic_writing(os_path, text=False) as f:
|
with self.atomic_writing(os_path, text=False) as f:
|
||||||
f.write(bcontent)
|
f.write(bcontent)
|
||||||
|
@ -131,7 +131,8 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
|
|||||||
self.post_save_hook(os_path=os_path, model=model, contents_manager=self)
|
self.post_save_hook(os_path=os_path, model=model, contents_manager=self)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log.error("Post-save hook failed o-n %s", os_path, exc_info=True)
|
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')
|
@validate('root_dir')
|
||||||
def _validate_root_dir(self, proposal):
|
def _validate_root_dir(self, proposal):
|
||||||
@ -487,7 +488,8 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
|
|||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)
|
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
|
validation_message = None
|
||||||
if model['type'] == 'notebook':
|
if model['type'] == 'notebook':
|
||||||
@ -584,7 +586,8 @@ class FileContentsManager(FileManagerMixin, ContentsManager):
|
|||||||
except web.HTTPError:
|
except web.HTTPError:
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
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):
|
def info_string(self):
|
||||||
return _("Serving notebooks from local directory: %s") % self.root_dir
|
return _("Serving notebooks from local directory: %s") % self.root_dir
|
||||||
|
@ -34,7 +34,7 @@ class LargeFileManager(FileContentsManager):
|
|||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)
|
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)
|
model = self.get(path, content=False)
|
||||||
|
|
||||||
@ -61,7 +61,7 @@ class LargeFileManager(FileContentsManager):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise web.HTTPError(
|
raise web.HTTPError(
|
||||||
400, u'Encoding error saving %s: %s' % (os_path, e)
|
400, u'Encoding error saving %s: %s' % (os_path, e)
|
||||||
)
|
) from e
|
||||||
|
|
||||||
with self.perm_to_403(os_path):
|
with self.perm_to_403(os_path):
|
||||||
if os.path.islink(os_path):
|
if os.path.islink(os_path):
|
||||||
|
@ -87,8 +87,8 @@ class KernelSpecHandler(APIHandler):
|
|||||||
kernel_name = url_unescape(kernel_name)
|
kernel_name = url_unescape(kernel_name)
|
||||||
try:
|
try:
|
||||||
spec = yield maybe_future(ksm.get_kernel_spec(kernel_name))
|
spec = yield maybe_future(ksm.get_kernel_spec(kernel_name))
|
||||||
except KeyError:
|
except KeyError as e:
|
||||||
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
|
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name) from e
|
||||||
if is_kernelspec_model(spec):
|
if is_kernelspec_model(spec):
|
||||||
model = spec
|
model = spec
|
||||||
else:
|
else:
|
||||||
|
@ -13,7 +13,7 @@ class NbconvertRootHandler(APIHandler):
|
|||||||
try:
|
try:
|
||||||
from nbconvert.exporters import base
|
from nbconvert.exporters import base
|
||||||
except ImportError as e:
|
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 = {}
|
res = {}
|
||||||
exporters = base.get_export_names()
|
exporters = base.get_export_names()
|
||||||
for exporter_name in exporters:
|
for exporter_name in exporters:
|
||||||
|
@ -44,13 +44,13 @@ class SessionRootHandler(APIHandler):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
path = model['path']
|
path = model['path']
|
||||||
except KeyError:
|
except KeyError as e:
|
||||||
raise web.HTTPError(400, "Missing field in JSON data: path")
|
raise web.HTTPError(400, "Missing field in JSON data: path") from e
|
||||||
|
|
||||||
try:
|
try:
|
||||||
mtype = model['type']
|
mtype = model['type']
|
||||||
except KeyError:
|
except KeyError as e:
|
||||||
raise web.HTTPError(400, "Missing field in JSON data: type")
|
raise web.HTTPError(400, "Missing field in JSON data: type") from e
|
||||||
|
|
||||||
name = model.get('name', None)
|
name = model.get('name', None)
|
||||||
kernel = model.get('kernel', {})
|
kernel = model.get('kernel', {})
|
||||||
@ -155,9 +155,9 @@ class SessionHandler(APIHandler):
|
|||||||
sm = self.session_manager
|
sm = self.session_manager
|
||||||
try:
|
try:
|
||||||
yield maybe_future(sm.delete_session(session_id))
|
yield maybe_future(sm.delete_session(session_id))
|
||||||
except KeyError:
|
except KeyError as e:
|
||||||
# the kernel was deleted but the session wasn't!
|
# 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.set_status(204)
|
||||||
self.finish()
|
self.finish()
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ class NotebookTestBase(TestCase):
|
|||||||
cls.fetch_url(url)
|
cls.fetch_url(url)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if not cls.notebook_thread.is_alive():
|
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)
|
time.sleep(POLL_INTERVAL)
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
@ -27,7 +27,7 @@ def quick_driver(lab=False):
|
|||||||
server = list(list_running_servers())[0]
|
server = list(list_running_servers())[0]
|
||||||
except IndexError as e:
|
except IndexError as e:
|
||||||
raise NoServerError('You need a server running before you can run '
|
raise NoServerError('You need a server running before you can run '
|
||||||
'this command')
|
'this command') from e
|
||||||
driver = Firefox()
|
driver = Firefox()
|
||||||
auth_url = '{url}?token={token}'.format(**server)
|
auth_url = '{url}?token={token}'.format(**server)
|
||||||
driver.get(auth_url)
|
driver.get(auth_url)
|
||||||
|
Loading…
Reference in New Issue
Block a user