Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give API connection errors a specific exception class #301

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Give API connection errors a specific exception class
  • Loading branch information
akx committed Jan 3, 2024
commit 8a3a769a22872a288e8b0237c63d06e5e6025eb6
16 changes: 10 additions & 6 deletions valohai_cli/api.py
Original file line number Diff line number Diff line change
@@ -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 @@
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

Check warning on line 84 in valohai_cli/api.py

Codecov / codecov/patch

valohai_cli/api.py#L81-L84

Added lines #L81 - L84 were not covered by tests

if handle_errors and resp.status_code >= 400:
cls = (APINotFoundError if resp.status_code == 404 else api_error_class)
4 changes: 4 additions & 0 deletions valohai_cli/exceptions.py
Original file line number Diff line number Diff line change
@@ -108,6 +108,10 @@ class PackageTooLarge(CLIException):
pass


class APIConnectionError(CLIException):
pass


class NoGitRepo(CLIException):
color = 'yellow'

Loading