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
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidRequestError(APIError):
|
||||||
|
"""The server rejected a request as invalid."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class MonitorError(APIError):
|
class MonitorError(APIError):
|
||||||
"""Monitoring a device failed, possibly because the monitoring
|
"""Monitoring a device failed, possibly because the monitoring
|
||||||
session failed and needs to be restarted.
|
session failed and needs to be restarted.
|
||||||
@ -95,6 +102,13 @@ class MonitorError(APIError):
|
|||||||
self.code = code
|
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):
|
def lgedm_post(url, data=None, access_token=None, session_id=None):
|
||||||
"""Make an HTTP request in the format used by the API servers.
|
"""Make an HTTP request in the format used by the API servers.
|
||||||
|
|
||||||
@ -123,14 +137,13 @@ def lgedm_post(url, data=None, access_token=None, session_id=None):
|
|||||||
# Check for API errors.
|
# Check for API errors.
|
||||||
if 'returnCd' in out:
|
if 'returnCd' in out:
|
||||||
code = out['returnCd']
|
code = out['returnCd']
|
||||||
if code != '0000':
|
if code == '0000':
|
||||||
|
pass
|
||||||
|
elif code in API_ERRORS:
|
||||||
|
raise API_ERRORS[code]()
|
||||||
|
else:
|
||||||
message = out['returnMsg']
|
message = out['returnMsg']
|
||||||
if code == "0102":
|
raise APIError(code, message)
|
||||||
raise NotLoggedInError()
|
|
||||||
elif code == "0106":
|
|
||||||
raise NotConnectedError()
|
|
||||||
else:
|
|
||||||
raise APIError(code, message)
|
|
||||||
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user