Skip to content

Commit 7098724

Browse files
committed
Add/rework exceptions
1 parent 78a0154 commit 7098724

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

airos/airos8.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
import aiohttp
1010

11-
from .exceptions import ConnectionFailedError, DataMissingError
11+
from .exceptions import (
12+
ConnectionAuthenticationError,
13+
ConnectionError,
14+
ConnectionSetupError,
15+
DataMissingError,
16+
)
1217

1318
logger = logging.getLogger(__name__)
1419

@@ -93,7 +98,7 @@ async def login(self) -> bool:
9398
) as response:
9499
if not response.cookies:
95100
logger.exception("Empty cookies after login, bailing out.")
96-
raise DataMissingError from None
101+
raise ConnectionSetupError from None
97102
else:
98103
for _, morsel in response.cookies.items():
99104
# If the AIROS_ cookie was parsed but isn't automatically added to the jar, add it manually
@@ -146,15 +151,15 @@ async def login(self) -> bool:
146151
logger.exception(
147152
"COOKIE JAR IS EMPTY after login POST. This is a major issue."
148153
)
149-
raise DataMissingError from None
154+
raise ConnectionSetupError from None
150155
for cookie in self.session.cookie_jar:
151156
if cookie.key.startswith("AIROS_"):
152157
airos_cookie_found = True
153158
if cookie.key == "ok":
154159
ok_cookie_found = True
155160

156161
if not airos_cookie_found and not ok_cookie_found:
157-
raise DataMissingError from None
162+
raise ConnectionSetupError from None
158163

159164
response_text = await response.text()
160165

@@ -170,16 +175,16 @@ async def login(self) -> bool:
170175
else:
171176
log = f"Login failed with status {response.status}. Full Response: {response.text}"
172177
logger.error(log)
173-
raise ConnectionFailedError from None
178+
raise ConnectionAuthenticationError from None
174179
except aiohttp.ClientError as err:
175180
logger.exception("Error during login")
176-
raise ConnectionFailedError from err
181+
raise ConnectionError from err
177182

178183
async def status(self) -> dict:
179184
"""Retrieve status from the device."""
180185
if not self.connected:
181186
logger.error("Not connected, login first")
182-
raise ConnectionFailedError from None
187+
raise ConnectionError from None
183188

184189
# --- Step 2: Verify authenticated access by fetching status.cgi ---
185190
authenticated_get_headers = {**self._common_headers}
@@ -205,4 +210,4 @@ async def status(self) -> dict:
205210
logger.error(log)
206211
except aiohttp.ClientError as err:
207212
logger.exception("Error during authenticated status.cgi call")
208-
raise ConnectionFailedError from err
213+
raise ConnectionError from err

airos/exceptions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ class AirOSException(Exception):
55
"""Base error class for this AirOS library."""
66

77

8-
class ConnectionFailedError(AirOSException):
8+
class ConnectionError(AirOSException):
99
"""Raised when unable to connect."""
1010

1111

12+
class ConnectionSetupError(AirOSException):
13+
"""Raised when unable to prepare authentication."""
14+
15+
16+
class ConnectionAuthenticationError(AirOSException):
17+
"""Raised when unable to authenticate."""
18+
19+
1220
class DataMissingError(AirOSException):
1321
"""Raised when expected data is missing."""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "airos"
7-
version = "0.0.7"
7+
version = "0.0.8"
88
license = "MIT"
99
description = "Ubiquity airOS module(s) for Python 3."
1010
readme = "README.md"

0 commit comments

Comments
 (0)