Skip to content

Commit e283b2a

Browse files
authored
fix(log)!: Invalid* exceptions replaced with CommandError; errors are logged with the API response (#137)
1 parent daf5329 commit e283b2a

File tree

3 files changed

+33
-66
lines changed

3 files changed

+33
-66
lines changed

src/elmo/api/client.py

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
from .decorators import require_lock, require_session
1212
from .exceptions import (
1313
CodeError,
14+
CommandError,
1415
CredentialError,
15-
InvalidInput,
16-
InvalidSector,
1716
InvalidToken,
1817
LockError,
1918
ParseError,
@@ -263,20 +262,14 @@ def arm(self, sectors=None):
263262
# Send the payload to arm sectors
264263
response = self._session.post(self._router.send_command, data=payload)
265264
response.raise_for_status()
266-
267-
errors = []
268-
# A not existing sector returns 200 with a fail state
269265
body = response.json()
270-
_LOGGER.debug(f"Client | Arming response: {body}")
271-
if not body[0]["Successful"]:
272-
errors.append(payload["ElementsIndexes"])
273266

274-
# Raise an exception if errors are detected
275-
if errors:
276-
invalid_sectors = ",".join(str(x) for x in errors)
277-
raise InvalidSector("Selected sectors don't exist: {}".format(invalid_sectors))
267+
# Errors returns 200 with "Successful == False" JSON key
268+
if not body[0]["Successful"]:
269+
_LOGGER.error(f"Client | Arming response: {body}")
270+
raise CommandError
278271

279-
_LOGGER.debug("Client | Arming successful")
272+
_LOGGER.debug(f"Client | Arming successful with response: {body}")
280273
return True
281274

282275
@require_session
@@ -323,20 +316,14 @@ def disarm(self, sectors=None):
323316
# Send the payload to disarm sectors
324317
response = self._session.post(self._router.send_command, data=payload)
325318
response.raise_for_status()
326-
327-
errors = []
328-
# A not existing sector returns 200 with a fail state
329319
body = response.json()
330-
_LOGGER.debug(f"Client | Disarming response: {body}")
331-
if not body[0]["Successful"]:
332-
errors.append(payload["ElementsIndexes"])
333320

334-
# Raise an exception if errors are detected
335-
if errors:
336-
invalid_sectors = ",".join(str(x) for x in errors)
337-
raise InvalidSector("Selected sectors don't exist: {}".format(invalid_sectors))
321+
# Errors returns 200 with "Successful == False" JSON key
322+
if not body[0]["Successful"]:
323+
_LOGGER.error(f"Client | Disarming response: {body}")
324+
raise CommandError
338325

339-
_LOGGER.debug("Client | Disarming successful")
326+
_LOGGER.debug(f"Client | Disarming successful with response: {body}")
340327
return True
341328

342329
@require_session
@@ -392,7 +379,7 @@ def exclude(self, inputs):
392379
# Raise an exception if errors are detected
393380
if errors:
394381
invalid_inputs = ",".join(str(x) for x in errors)
395-
raise InvalidInput("Selected inputs don't exist: {}".format(invalid_inputs))
382+
raise CommandError("Selected inputs don't exist: {}".format(invalid_inputs))
396383

397384
_LOGGER.debug("Client | Excluding successful")
398385
return True
@@ -450,7 +437,7 @@ def include(self, inputs):
450437
# Raise an exception if errors are detected
451438
if errors:
452439
invalid_inputs = ",".join(str(x) for x in errors)
453-
raise InvalidInput("Selected inputs don't exist: {}".format(invalid_inputs))
440+
raise CommandError("Selected inputs don't exist: {}".format(invalid_inputs))
454441

455442
_LOGGER.debug("Client | Including successful")
456443
return True
@@ -488,24 +475,17 @@ def turn_on(self, outputs):
488475
"sessionId": self._session_id,
489476
}
490477

491-
errors = []
492-
493478
# Send turn on request
494479
response = self._session.post(self._router.send_command, data=payload)
495480
response.raise_for_status()
496-
497-
# A not existing output returns 200 with a fail state
498481
body = response.json()
499-
_LOGGER.debug(f"Client | Turning on response: {body}")
500-
if not body[0]["Successful"]:
501-
errors.append(payload["ElementsIndexes"])
502482

503-
# Raise an exception if errors are detected
504-
if errors:
505-
invalid_outputs = ",".join(str(x) for x in errors)
506-
raise InvalidInput("Selected outputs don't exist: {}".format(invalid_outputs))
483+
# Errors returns 200 with "Successful == False" JSON key
484+
if not body[0]["Successful"]:
485+
_LOGGER.error(f"Client | Turning on response: {body}")
486+
raise CommandError
507487

508-
_LOGGER.debug("Client | Turning on output successful")
488+
_LOGGER.debug(f"Client | Turning on successful with response: {body}")
509489
return True
510490

511491
@require_session
@@ -544,20 +524,14 @@ def turn_off(self, outputs):
544524
# Send turn off request
545525
response = self._session.post(self._router.send_command, data=payload)
546526
response.raise_for_status()
547-
548-
errors = []
549-
# A not existing output returns 200 with a fail state
550527
body = response.json()
551-
_LOGGER.debug(f"Client | Turning off response: {body}")
552-
if not body[0]["Successful"]:
553-
errors.append(payload["ElementsIndexes"])
554528

555-
# Raise an exception if errors are detected
556-
if errors:
557-
invalid_outputs = ",".join(str(x) for x in errors)
558-
raise InvalidInput("Selected outputs don't exist: {}".format(invalid_outputs))
529+
# Errors returns 200 with "Successful == False" JSON key
530+
if not body[0]["Successful"]:
531+
_LOGGER.error(f"Client | Turning on response: {body}")
532+
raise CommandError
559533

560-
_LOGGER.debug("Client | Turning off output successful")
534+
_LOGGER.debug(f"Client | Turning on successful with response: {body}")
561535
return True
562536

563537
@lru_cache(maxsize=1)

src/elmo/api/exceptions.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,7 @@ class CodeError(APIException):
7070
default_message = "Digited panel code is not correct"
7171

7272

73-
class InvalidSector(APIException):
74-
"""Exception raised when armed/disarmed sector doesn't exist."""
73+
class CommandError(APIException):
74+
"""Exception raised when the API returns an error response after issuing a command."""
7575

76-
default_message = "Selected sector doesn't exist."
77-
78-
79-
class InvalidInput(APIException):
80-
"""Exception raised when included/excluded input doesn't exist."""
81-
82-
default_message = "Selected input doesn't exist."
76+
default_message = "An error occurred while executing the command."

tests/test_client.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
from elmo.api.client import ElmoClient
99
from elmo.api.exceptions import (
1010
CodeError,
11+
CommandError,
1112
CredentialError,
12-
InvalidInput,
13-
InvalidSector,
1413
InvalidToken,
1514
LockError,
1615
LockNotAcquired,
@@ -889,7 +888,7 @@ def test_client_arm_fails_wrong_sector(server):
889888
client._session_id = "test"
890889
client._lock.acquire()
891890
# Test
892-
with pytest.raises(InvalidSector):
891+
with pytest.raises(CommandError):
893892
assert client.arm([200])
894893

895894

@@ -1041,7 +1040,7 @@ def test_client_disarm_fails_wrong_sector(server):
10411040
client._session_id = "test"
10421041
client._lock.acquire()
10431042
# Test
1044-
with pytest.raises(InvalidSector):
1043+
with pytest.raises(CommandError):
10451044
assert client.disarm([200])
10461045

10471046

@@ -1172,7 +1171,7 @@ def test_client_include_fails_wrong_input(server):
11721171
client._session_id = "test"
11731172
client._lock.acquire()
11741173
# Test
1175-
with pytest.raises(InvalidInput):
1174+
with pytest.raises(CommandError):
11761175
assert client.include([9000])
11771176

11781177

@@ -1303,7 +1302,7 @@ def test_client_exclude_fails_wrong_input(server):
13031302
client._session_id = "test"
13041303
client._lock.acquire()
13051304
# Test
1306-
with pytest.raises(InvalidInput):
1305+
with pytest.raises(CommandError):
13071306
assert client.exclude([9000])
13081307

13091308

@@ -1414,7 +1413,7 @@ def test_client_fails_wrong_input(self, server):
14141413
client = ElmoClient(base_url="https://example.com", domain="domain")
14151414
client._session_id = "test"
14161415
# Test
1417-
with pytest.raises(InvalidInput):
1416+
with pytest.raises(CommandError):
14181417
assert client.turn_on([9000])
14191418

14201419
def test_client_fails_unknown_error(self, server):
@@ -1523,7 +1522,7 @@ def test_client_fails_wrong_input(self, server):
15231522
client = ElmoClient(base_url="https://example.com", domain="domain")
15241523
client._session_id = "test"
15251524
# Test
1526-
with pytest.raises(InvalidInput):
1525+
with pytest.raises(CommandError):
15271526
assert client.turn_off([9000])
15281527

15291528
def test_client_fails_unknown_error(self, server):

0 commit comments

Comments
 (0)