From 8a3a769a22872a288e8b0237c63d06e5e6025eb6 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 3 Jan 2024 15:14:22 +0200 Subject: [PATCH] Give API connection errors a specific exception class --- valohai_cli/api.py | 16 ++++++++++------ valohai_cli/exceptions.py | 4 ++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/valohai_cli/api.py b/valohai_cli/api.py index 3c006b94..f146714d 100644 --- a/valohai_cli/api.py +++ b/valohai_cli/api.py @@ -8,7 +8,12 @@ from requests.models import PreparedRequest, Request, Response from valohai_cli import __version__ as VERSION -from valohai_cli.exceptions import APIError, APINotFoundError, CLIException, NotLoggedIn +from valohai_cli.exceptions import ( + APIConnectionError, + APIError, + APINotFoundError, + NotLoggedIn, +) from valohai_cli.settings import settings from valohai_cli.utils import force_text @@ -72,12 +77,11 @@ def request(self, method: str, url: str, **kwargs: Any) -> Response: # type: ig try: resp = super().request(method, url, **kwargs) except requests.ConnectionError as ce: - host = urlparse(ce.request.url).netloc if ce.request else self.base_netloc if 'Connection refused' in str(ce): - raise CLIException( - f'Unable to connect to {host!r} (connection refused); try again soon.' - ) from ce - raise + host = urlparse(ce.request.url).netloc if ce.request else self.base_netloc + msg = f'Unable to connect to {host!r} (connection refused); try again soon.' + raise APIConnectionError(msg) from ce + raise APIConnectionError(str(ce)) from ce if handle_errors and resp.status_code >= 400: cls = (APINotFoundError if resp.status_code == 404 else api_error_class) diff --git a/valohai_cli/exceptions.py b/valohai_cli/exceptions.py index 7f0b3b7f..715c086c 100644 --- a/valohai_cli/exceptions.py +++ b/valohai_cli/exceptions.py @@ -108,6 +108,10 @@ class PackageTooLarge(CLIException): pass +class APIConnectionError(CLIException): + pass + + class NoGitRepo(CLIException): color = 'yellow'