Skip to content

Commit 80ad657

Browse files
authored
Merge pull request #19 from TheBassEngineer/call_api-add_named_errors
Add named errors to call_api
2 parents 9312f07 + 036c853 commit 80ad657

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

decora_wifi/__init__.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ def call_api(self, api, payload=None, method='get'):
3232
if (method != 'get' and method != 'post' and
3333
method != 'put' and method != 'delete'):
3434
msg = "Tried decora.call_api with bad method: {0}"
35-
raise ValueError(msg.format(method))
35+
raise BadMethodError(msg.format(method))
3636

3737
if self.user is None and api != '/Person/login':
38-
raise ValueError('Tried an API call without a login.')
38+
raise NoLoginError('Tried an API call without a login.')
3939

4040
uri = self.LEVITON_ROOT + api
4141

@@ -51,14 +51,14 @@ def call_api(self, api, payload=None, method='get'):
5151
if response.status_code == 401 or response.status_code == 403:
5252
# Maybe we got logged out? Let's try logging in.
5353
if self.login(self._email, self._password) is None:
54-
raise ValueError("Auth expired and unable to refresh")
54+
raise AuthExpiredError("Auth expired and unable to refresh")
5555
# Retry the request...
5656
response = getattr(self._session, method)(uri, data=payload_json)
5757

5858
if response.status_code != 200 and response.status_code != 204:
5959
msg = "myLeviton API call ({0}) failed: {1}, {2}".format(
6060
api, response.status_code, response.text)
61-
raise ValueError(msg)
61+
raise ApiCallFailedError(msg)
6262

6363
if response.text is not None and len(response.text) > 0:
6464
return json.loads(response.text)
@@ -89,3 +89,23 @@ def login(self, email, password):
8989
self.user.refresh()
9090

9191
return self.user
92+
93+
class DecoraWiFiError(ValueError):
94+
"""Base class for named errors in decora_wifi."""
95+
pass
96+
97+
class BadMethodError(DecoraWiFiError):
98+
"""Tried a decora_wifi api call with an unsupported method."""
99+
pass
100+
101+
class NoLoginError(DecoraWiFiError):
102+
"""Tried a decora_wifi api call without a logged-in session."""
103+
pass
104+
105+
class AuthExpiredError(DecoraWiFiError):
106+
"""API Session authentication expired and unable to refresh."""
107+
pass
108+
109+
class ApiCallFailedError(DecoraWiFiError):
110+
"""API Call failed (returned a code other than 200 or 204)."""
111+
pass

0 commit comments

Comments
 (0)