Skip to content

Commit dbb5dac

Browse files
committed
fix: Improved check for the PodmanConnectionError
1 parent a8c4fec commit dbb5dac

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

podman/client.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,38 @@ def __init__(self, **kwargs) -> None:
6161
super().__init__()
6262
config = PodmanConfig()
6363

64-
api_kwargs = kwargs.copy()
64+
self.api_kwargs = kwargs.copy()
6565

66-
if "connection" in api_kwargs:
67-
connection = config.services[api_kwargs.get("connection")]
68-
api_kwargs["base_url"] = connection.url.geturl()
66+
if "connection" in self.api_kwargs:
67+
connection = config.services[self.api_kwargs.get("connection")]
68+
self.api_kwargs["base_url"] = connection.url.geturl()
6969

7070
# Override configured identity, if provided in arguments
71-
api_kwargs["identity"] = kwargs.get("identity", str(connection.identity))
72-
elif "base_url" not in api_kwargs:
71+
self.api_kwargs["identity"] = kwargs.get("identity", str(connection.identity))
72+
elif "base_url" not in self.api_kwargs:
7373
path = str(Path(get_runtime_dir()) / "podman" / "podman.sock")
74-
api_kwargs["base_url"] = "http+unix://" + path
75-
self.api = APIClient(**api_kwargs)
74+
self.api_kwargs["base_url"] = "http+unix://" + path
7675

77-
# Check if the connection to the Podman service is successful
7876
try:
79-
SystemManager(client=self.api).version()
77+
self.api = APIClient(**self.api_kwargs)
78+
response = self.api.get("_ping")
79+
80+
if response.status_code != 200:
81+
raise PodmanConnectionError(
82+
message=f"Unexpected response from Podman service: {response.status_code}",
83+
environment=os.environ,
84+
host=self.api_kwargs.get("base_url"),
85+
original_error=None,
86+
)
87+
except PodmanConnectionError:
88+
raise
8089
except Exception as e:
81-
error_msg = "Failed to connect to Podman service"
8290
raise PodmanConnectionError(
83-
message=error_msg,
91+
message=f"Failed to connect to Podman service: {str(e)}",
8492
environment=os.environ,
85-
host=api_kwargs.get("base_url"),
93+
host=self.api_kwargs.get("base_url"),
8694
original_error=e,
87-
)
95+
) from e
8896

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

0 commit comments

Comments
 (0)