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

feat: Enhance environment connection error handling #512

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions podman/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path
from typing import Any, Optional

from podman.errors.exceptions import APIError, PodmanConnectionError
from podman.api import cached_property
from podman.api.client import APIClient
from podman.api.path_utils import get_runtime_dir
Expand Down Expand Up @@ -73,6 +74,39 @@ def __init__(self, **kwargs) -> None:
api_kwargs["base_url"] = "http+unix://" + path
self.api = APIClient(**api_kwargs)

self._verify_connection()

def _verify_connection(self):
"""Verify connection to Podman daemon during initialization.

Raises:
PodmanException: If unable to connect to Podman daemon
"""
try:
# Attempt to get version info to verify connection
self.version()
except APIError as e:
if "No such file or directory" in str(e):
raise PodmanConnectionError(
"Error while connecting to Podman daemon: "
f"Could not find socket file - {str(e)}"
) from e
raise PodmanConnectionError(f"Error while connecting to Podman daemon: {str(e)}") from e
except Exception as e:
raise PodmanConnectionError(f"Error while connecting to Podman daemon: {str(e)}") from e

def __enter__(self) -> "PodmanClient":
return self

def __exit__(self, exc_type, exc_value, traceback) -> None:
self.close()

def __enter__(self) -> "PodmanClient":
return self

def __exit__(self, exc_type, exc_value, traceback) -> None:
self.close()

def __enter__(self) -> "PodmanClient":
return self

Expand Down
2 changes: 2 additions & 0 deletions podman/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'NotFoundError',
'PodmanError',
'StreamParseError',
'PodmanConnectionError',
]

try:
Expand All @@ -34,6 +35,7 @@
NotFound,
PodmanError,
StreamParseError,
PodmanConnectionError,
)
except ImportError:
pass
Expand Down
4 changes: 4 additions & 0 deletions podman/errors/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ class InvalidArgument(PodmanError):
"""Parameter to method/function was not valid."""


class PodmanConnectionError(PodmanError):
"""Exception raised when connection to Podman service fails using environment configuration."""


class StreamParseError(RuntimeError):
def __init__(self, reason):
self.msg = reason
7 changes: 6 additions & 1 deletion podman/tests/integration/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import podman.tests.integration.base as base
from podman import PodmanClient
from podman.errors import APIError
from podman.errors import APIError, PodmanConnectionError


class SystemIntegrationTest(base.IntegrationTest):
Expand Down Expand Up @@ -64,3 +64,8 @@ def test_login(self):
def test_from_env(self):
"""integration: from_env() no error"""
PodmanClient.from_env()

def test_from_env_exceptions(self):
"""integration: from_env() returns exceptions"""
with self.assertRaises(PodmanConnectionError):
PodmanClient.from_env(base_url="unix:///path/to/nonexistent.sock")
Loading