mirror of
https://github.com/no2chem/wideq.git
synced 2025-05-29 05:20:24 -07:00
Refactor API error handling
Use a declarative style for mapping errors. Also add a new one for an "invalid request" message.
This commit is contained in:
parent
07b6a1ca21
commit
3ac6cab147
@ -85,6 +85,13 @@ class TokenError(APIError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidRequestError(APIError):
|
||||
"""The server rejected a request as invalid."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
class MonitorError(APIError):
|
||||
"""Monitoring a device failed, possibly because the monitoring
|
||||
session failed and needs to be restarted.
|
||||
@ -95,6 +102,13 @@ class MonitorError(APIError):
|
||||
self.code = code
|
||||
|
||||
|
||||
API_ERRORS = {
|
||||
"0102": NotLoggedInError,
|
||||
"0106": NotConnectedError,
|
||||
9000: InvalidRequestError, # Surprisingly, an integer (not a string).
|
||||
}
|
||||
|
||||
|
||||
def lgedm_post(url, data=None, access_token=None, session_id=None):
|
||||
"""Make an HTTP request in the format used by the API servers.
|
||||
|
||||
@ -123,13 +137,12 @@ def lgedm_post(url, data=None, access_token=None, session_id=None):
|
||||
# Check for API errors.
|
||||
if 'returnCd' in out:
|
||||
code = out['returnCd']
|
||||
if code != '0000':
|
||||
message = out['returnMsg']
|
||||
if code == "0102":
|
||||
raise NotLoggedInError()
|
||||
elif code == "0106":
|
||||
raise NotConnectedError()
|
||||
if code == '0000':
|
||||
pass
|
||||
elif code in API_ERRORS:
|
||||
raise API_ERRORS[code]()
|
||||
else:
|
||||
message = out['returnMsg']
|
||||
raise APIError(code, message)
|
||||
|
||||
return out
|
||||
|
Loading…
x
Reference in New Issue
Block a user