Skip to content

Commit 9dd2a9a

Browse files
authored
Merge pull request #245 from cool-RR/2020-06-20-raise-from
Fix exception causes in 5 modules
2 parents ad3bbca + d4a5616 commit 9dd2a9a

File tree

5 files changed

+28
-22
lines changed

5 files changed

+28
-22
lines changed

jupyter_server/base/handlers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,10 @@ def get_json_body(self):
470470
body = self.request.body.strip().decode(u'utf-8')
471471
try:
472472
model = json.loads(body)
473-
except Exception:
473+
except Exception as e:
474474
self.log.debug("Bad JSON: %r", body)
475475
self.log.error("Couldn't parse JSON", exc_info=True)
476-
raise web.HTTPError(400, u'Invalid JSON in body of request')
476+
raise web.HTTPError(400, u'Invalid JSON in body of request') from e
477477
return model
478478

479479
def write_error(self, status_code, **kwargs):

jupyter_server/extension/handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def static_url(self, path, include_host=None, **kwargs):
7575
raise Exception(
7676
"This extension doesn't have any static paths listed. Check that the "
7777
"extension's `static_paths` trait is set."
78-
)
78+
) from e
7979
else:
8080
raise e
8181

jupyter_server/extension/serverextension.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ def _get_load_jupyter_server_extension(obj):
149149
func = getattr(obj, '_load_jupyter_server_extension')
150150
except AttributeError:
151151
func = getattr(obj, 'load_jupyter_server_extension')
152-
except:
153-
raise ExtensionLoadingError("_load_jupyter_server_extension function was not found.")
152+
except BaseException:
153+
raise ExtensionLoadingError(
154+
"_load_jupyter_server_extension function was not found."
155+
) from e
154156
return func
155157

156158

@@ -178,8 +180,8 @@ def validate_server_extension(name):
178180
try:
179181
mod, metadata = _get_server_extension_metadata(name)
180182
version = getattr(mod, '__version__', '')
181-
except ImportError:
182-
raise ExtensionValidationError('{} is not importable.'.format(name))
183+
except ImportError as e:
184+
raise ExtensionValidationError('{} is not importable.'.format(name)) from e
183185

184186
try:
185187
for item in metadata:
@@ -193,8 +195,10 @@ def validate_server_extension(name):
193195
else:
194196
raise AttributeError
195197
# If the extension does not have a `load_jupyter_server_extension` function, raise exception.
196-
except AttributeError:
197-
raise ExtensionValidationError('Found "{}" module but cannot load it.'.format(name))
198+
except AttributeError as e:
199+
raise ExtensionValidationError(
200+
'Found "{}" module but cannot load it.'.format(name)
201+
) from e
198202
return version
199203

200204

jupyter_server/gateway/managers.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,18 +279,18 @@ async def gateway_request(endpoint, **kwargs):
279279
# or the server is not running.
280280
# NOTE: We do this here since this handler is called during the Notebook's startup and subsequent refreshes
281281
# of the tree view.
282-
except ConnectionRefusedError:
282+
except ConnectionRefusedError as e:
283283
raise web.HTTPError(503, "Connection refused from Gateway server url '{}'. "
284-
"Check to be sure the Gateway instance is running.".format(GatewayClient.instance().url))
284+
"Check to be sure the Gateway instance is running.".format(GatewayClient.instance().url)) from e
285285
except HTTPError as e:
286286
# This can occur if the host is valid (e.g., foo.com) but there's nothing there.
287287
raise web.HTTPError(e.code, "Error attempting to connect to Gateway server url '{}'. "
288288
"Ensure gateway url is valid and the Gateway instance is running.".
289-
format(GatewayClient.instance().url))
290-
except gaierror:
289+
format(GatewayClient.instance().url)) from e
290+
except gaierror as e:
291291
raise web.HTTPError(404, "The Gateway server specified in the gateway_url '{}' doesn't appear to be valid. "
292292
"Ensure gateway url is valid and the Gateway instance is running.".
293-
format(GatewayClient.instance().url))
293+
format(GatewayClient.instance().url)) from e
294294

295295
return response
296296

@@ -552,8 +552,10 @@ async def get_kernel_spec(self, kernel_name, **kwargs):
552552
if error.status_code == 404:
553553
# Convert not found to KeyError since that's what the Notebook handler expects
554554
# message is not used, but might as well make it useful for troubleshooting
555-
raise KeyError('kernelspec {kernel_name} not found on Gateway server at: {gateway_url}'.
556-
format(kernel_name=kernel_name, gateway_url=GatewayClient.instance().url))
555+
raise KeyError(
556+
'kernelspec {kernel_name} not found on Gateway server at: {gateway_url}'.
557+
format(kernel_name=kernel_name, gateway_url=GatewayClient.instance().url)
558+
) from error
557559
else:
558560
raise
559561
else:

jupyter_server/serverapp.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
# check for tornado 3.1.0
4545
try:
4646
import tornado
47-
except ImportError:
48-
raise ImportError(_("The Jupyter Server requires tornado >= 4.0"))
47+
except ImportError as e:
48+
raise ImportError(_("The Jupyter Server requires tornado >= 4.0")) from e
4949
try:
5050
version_info = tornado.version_info
51-
except AttributeError:
52-
raise ImportError(_("The Jupyter Server requires tornado >= 4.0, but you have < 1.1.0"))
51+
except AttributeError as e:
52+
raise ImportError(_("The Jupyter Server requires tornado >= 4.0, but you have < 1.1.0")) from e
5353
if version_info < (4,0):
5454
raise ImportError(_("The Jupyter Server requires tornado >= 4.0, but you have %s") % tornado.version)
5555

@@ -1681,12 +1681,12 @@ def http_server(self):
16811681
"""An instance of Tornado's HTTPServer class for the Server Web Application."""
16821682
try:
16831683
return self._http_server
1684-
except AttributeError:
1684+
except AttributeError as e:
16851685
raise AttributeError(
16861686
'An HTTPServer instance has not been created for the '
16871687
'Server Web Application. To create an HTTPServer for this '
16881688
'application, call `.init_httpserver()`.'
1689-
)
1689+
) from e
16901690

16911691
def init_httpserver(self):
16921692
"""Creates an instance of a Tornado HTTPServer for the Server Web Application

0 commit comments

Comments
 (0)