Handle internal exception, ignore API error
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
2528809d17
commit
bd529fddda
@ -50,6 +50,7 @@ def main(\
|
||||
client.loop_forever()
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
#deactivate urllib3 logger too verbose
|
||||
logger = logging.getLogger('urllib3')
|
||||
|
@ -31,10 +31,12 @@ class Yamaha:
|
||||
def available_senarios(self):
|
||||
return SENARIOS
|
||||
|
||||
@exception_handler
|
||||
def senario_activate(self, senario: Senario):
|
||||
for action in senario.activate:
|
||||
self.request(action)
|
||||
|
||||
@exception_handler
|
||||
def senario_deactivate(self, senario: Senario):
|
||||
for action in senario.deactivate:
|
||||
self.request(action)
|
||||
@ -44,11 +46,17 @@ class Yamaha:
|
||||
r = self._device.request(request)
|
||||
except requests.exceptions.ReadTimeout:
|
||||
raise ConnectionTimeoutError()
|
||||
|
||||
try:
|
||||
r_json = r.json()
|
||||
except json.decoder.JSONDecodeError:
|
||||
raise ApiInvalidResponseError()
|
||||
|
||||
if r_json['response_code'] != 0:
|
||||
raise ApiError(r.url, r_json['response_code'])
|
||||
return r_json
|
||||
|
||||
@exception_handler
|
||||
def search_senario(self, name: str):
|
||||
for s in self.available_senarios:
|
||||
if s.name() == name.lower():
|
||||
@ -56,6 +64,7 @@ class Yamaha:
|
||||
else:
|
||||
return None
|
||||
|
||||
@exception_handler
|
||||
def handle_senario(self, client, userdata, msg):
|
||||
senario_name =msg.payload.decode().lower()
|
||||
|
||||
@ -73,10 +82,12 @@ class Yamaha:
|
||||
|
||||
self._client.publish('yamaha/stat/senario', senario_name)
|
||||
|
||||
@exception_handler
|
||||
def handle_senario_available(self, client, userdata, msg):
|
||||
self._client.publish('yamaha/stat/senario/available',
|
||||
json.dumps([s.__name__.lower() for s in self.available_senarios]))
|
||||
|
||||
@exception_handler
|
||||
def handle_volume(self, client, userdata, msg):
|
||||
param =msg.payload.decode().lower()
|
||||
|
||||
@ -95,6 +106,7 @@ class Yamaha:
|
||||
|
||||
self._client.publish('yamaha/stat/volume', json.dumps(msg))
|
||||
|
||||
@exception_handler
|
||||
def handle_power(self, client, userdata, msg):
|
||||
param =msg.payload.decode().lower()
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
import logging
|
||||
|
||||
class Error(Exception):
|
||||
"""Base class for exceptions in this module."""
|
||||
@ -8,5 +9,18 @@ class ApiError(Error):
|
||||
self.request = request_url
|
||||
self.response_code = response_code
|
||||
|
||||
class ApiInvalidResponseError(Error):
|
||||
pass
|
||||
|
||||
class ConnectionTimeoutError(Error):
|
||||
pass
|
||||
|
||||
def exception_handler(func):
|
||||
def inner_function(*args, **kwargs):
|
||||
try:
|
||||
func(*args, **kwargs)
|
||||
except Error:
|
||||
logger = logging.getLogger('')
|
||||
logger.exception("Internal error occur")
|
||||
return inner_function
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user