From dbd63fd3969108bea19cbc844cb2e7b6189ab589 Mon Sep 17 00:00:00 2001 From: BrodyWanner Date: Tue, 7 Mar 2023 12:04:03 -0600 Subject: [PATCH 1/2] Added error propagation to gateway_request function --- jupyter_server/gateway/gateway_client.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/jupyter_server/gateway/gateway_client.py b/jupyter_server/gateway/gateway_client.py index 8444a4cddb..bade03ffa6 100644 --- a/jupyter_server/gateway/gateway_client.py +++ b/jupyter_server/gateway/gateway_client.py @@ -728,9 +728,20 @@ async def gateway_request(endpoint: str, **kwargs: ty.Any) -> HTTPResponse: # NOTE: We do this here since this handler is called during the server's startup and subsequent refreshes # of the tree view. except HTTPClientError as e: + try: + error_payload = json.loads(e.response.body) + error_reason = ( + error_payload.get("reason") + or f"Exception while attempting to connect to Gateway server url '{GatewayClient.instance().url}'" + ) + error_message = error_payload.get("message") or e.message + except json.decoder.JSONDecodeError: + error_reason = e.response.body.decode() + error_message = e.message + raise web.HTTPError( e.code, - f"Error attempting to connect to Gateway server url '{GatewayClient.instance().url}'. " + f"Error from Gateway: [{error_message}] {error_reason}. " "Ensure gateway url is valid and the Gateway instance is running.", ) from e except ConnectionError as e: From e11e27e5396b55f542ff12831d5f2fb5840c48c7 Mon Sep 17 00:00:00 2001 From: BrodyWanner Date: Tue, 7 Mar 2023 13:00:51 -0600 Subject: [PATCH 2/2] Added logic to account for response possibly being None --- jupyter_server/gateway/gateway_client.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/jupyter_server/gateway/gateway_client.py b/jupyter_server/gateway/gateway_client.py index bade03ffa6..1d2b420f50 100644 --- a/jupyter_server/gateway/gateway_client.py +++ b/jupyter_server/gateway/gateway_client.py @@ -728,16 +728,15 @@ async def gateway_request(endpoint: str, **kwargs: ty.Any) -> HTTPResponse: # NOTE: We do this here since this handler is called during the server's startup and subsequent refreshes # of the tree view. except HTTPClientError as e: - try: - error_payload = json.loads(e.response.body) - error_reason = ( - error_payload.get("reason") - or f"Exception while attempting to connect to Gateway server url '{GatewayClient.instance().url}'" - ) - error_message = error_payload.get("message") or e.message - except json.decoder.JSONDecodeError: - error_reason = e.response.body.decode() - error_message = e.message + error_reason = f"Exception while attempting to connect to Gateway server url '{GatewayClient.instance().url}'" + error_message = e.message + if e.response: + try: + error_payload = json.loads(e.response.body) + error_reason = error_payload.get("reason") or error_reason + error_message = error_payload.get("message") or error_message + except json.decoder.JSONDecodeError: + error_reason = e.response.body.decode() raise web.HTTPError( e.code,