Skip to content

Commit 28237ec

Browse files
committed
fix: Update the exception handling behavior to account for ambiguity
1 parent 2d04e37 commit 28237ec

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

roborock/mqtt/roborock_session.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
from aiomqtt import MqttCodeError, MqttError, TLSParameters
1919

2020
from roborock.callbacks import CallbackMap
21-
from roborock.exceptions import RoborockInvalidCredentials
2221

23-
from .session import MqttParams, MqttSession, MqttSessionException
22+
from .session import MqttParams, MqttSession, MqttSessionException, MqttSessionUnauthorized
2423

2524
_LOGGER = logging.getLogger(__name__)
2625
_MQTT_LOGGER = logging.getLogger(f"{__name__}.aiomqtt")
@@ -96,7 +95,7 @@ async def start(self) -> None:
9695
await start_future
9796
except MqttCodeError as err:
9897
if err.rc == MqttReasonCode.RC_ERROR_UNAUTHORIZED:
99-
raise RoborockInvalidCredentials(f"Authorization error starting MQTT session: {err}") from err
98+
raise MqttSessionUnauthorized(f"Authorization error starting MQTT session: {err}") from err
10099
raise MqttSessionException(f"Error starting MQTT session: {err}") from err
101100
except MqttError as err:
102101
raise MqttSessionException(f"Error starting MQTT session: {err}") from err

roborock/mqtt/session.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ async def close(self) -> None:
6464

6565

6666
class MqttSessionException(RoborockException):
67-
"""Raised when there is an error communicating with MQTT.
67+
"""Raised when there is an error communicating with MQTT."""
6868

69-
Note that not all exceptions raised by the MQTT session are of this type
70-
as other `RoborockException`s may be raised for specific error conditions
71-
such as authentication errors.
69+
70+
class MqttSessionUnauthorized(RoborockException):
71+
"""Raised when there is an authorization error communicating with MQTT.
72+
73+
This error may be raised in multiple scenarios so there is not a well
74+
defined behavior for how the caller should behave. The two cases are:
75+
- Rate limiting is in effect and the caller should retry after some time.
76+
- The credentials are invalid and the caller needs to obtain new credentials
77+
78+
However, it is observed that obtaining new credentials may resolve the
79+
issue in both cases.
7280
"""

tests/mqtt/test_roborock_session.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
import paho.mqtt.client as mqtt
1212
import pytest
1313

14-
from roborock.exceptions import RoborockInvalidCredentials
1514
from roborock.mqtt.roborock_session import RoborockMqttSession, create_mqtt_session
16-
from roborock.mqtt.session import MqttParams, MqttSessionException
15+
from roborock.mqtt.session import MqttParams, MqttSessionException, MqttSessionUnauthorized
1716
from tests import mqtt_packet
1817
from tests.conftest import FakeSocketHandler
1918

@@ -379,7 +378,7 @@ async def test_idle_timeout_multiple_callbacks(mock_mqtt_client: AsyncMock) -> N
379378
),
380379
(
381380
aiomqtt.MqttCodeError(rc=135),
382-
RoborockInvalidCredentials,
381+
MqttSessionUnauthorized,
383382
"Authorization error starting MQTT session",
384383
),
385384
(

0 commit comments

Comments
 (0)