From aa73eb43cd69ed35967a03715884d3d7d579e709 Mon Sep 17 00:00:00 2001 From: Shuichiro MAKIGAKI Date: Fri, 15 Nov 2019 16:58:36 +0900 Subject: [PATCH] Fix packages of exceptions catched by gateway manager `gateway_request` in `gateway.manager` raises `tornado.web.HTTPError` exceptions, but the callers, such as `GatewayKernelManager.get_kernel`, catch `tornado.httpclient.HTTPError`, instead of `tornado.web.HTTPError`. Therefore, the callers can not handle exceptions during gateway interactions. This causes that, for example, when Jupyter Enterprise Gateway culled a kernel by idle timeout, the gateway manager can not handle the kernel's absent appropriately. As a result, notebook users see ambiguous "Kernel Error" and can not restart the kernel or start a new kernel. --- notebook/gateway/managers.py | 18 +++++++++--------- notebook/tests/test_gateway.py | 3 ++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/notebook/gateway/managers.py b/notebook/gateway/managers.py index 7f2ff8695..d49960753 100644 --- a/notebook/gateway/managers.py +++ b/notebook/gateway/managers.py @@ -283,12 +283,12 @@ def gateway_request(endpoint, **kwargs): 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 HTTPError: + except HTTPError as e: # This can occur if the host is valid (e.g., foo.com) but there's nothing there. - raise web.HTTPError(504, "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.". format(GatewayClient.instance().url)) - except gaierror as e: + 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)) @@ -390,8 +390,8 @@ class GatewayKernelManager(MappingKernelManager): self.log.debug("Request kernel at: %s" % kernel_url) try: response = yield gateway_request(kernel_url, method='GET') - except HTTPError as error: - if error.code == 404: + except web.HTTPError as error: + if error.status_code == 404: self.log.warn("Kernel not found at: %s" % kernel_url) self.remove_kernel(kernel_id) kernel = None @@ -559,8 +559,8 @@ class GatewayKernelSpecManager(KernelSpecManager): self.log.debug("Request kernel spec at: %s" % kernel_spec_url) try: response = yield gateway_request(kernel_spec_url, method='GET') - except HTTPError as error: - if error.code == 404: + except web.HTTPError as error: + 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}'. @@ -587,8 +587,8 @@ class GatewayKernelSpecManager(KernelSpecManager): self.log.debug("Request kernel spec resource '{}' at: {}".format(path, kernel_spec_resource_url)) try: response = yield gateway_request(kernel_spec_resource_url, method='GET') - except HTTPError as error: - if error.code == 404: + except web.HTTPError as error: + if error.status_code == 404: kernel_spec_resource = None else: raise diff --git a/notebook/tests/test_gateway.py b/notebook/tests/test_gateway.py index 46ed885f5..d0cc84b09 100644 --- a/notebook/tests/test_gateway.py +++ b/notebook/tests/test_gateway.py @@ -8,7 +8,8 @@ from unittest.mock import patch import nose.tools as nt from tornado import gen -from tornado.httpclient import HTTPRequest, HTTPResponse, HTTPError +from tornado.web import HTTPError +from tornado.httpclient import HTTPRequest, HTTPResponse from notebook.gateway.managers import GatewayClient from notebook.utils import maybe_future