Skip to content

Commit

Permalink
handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Jun 17, 2023
1 parent ea38aae commit 3fff1c9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions routes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from utils import mqtt

import requests
import logging

logger = logging.getLogger(__name__)

api_bp = Blueprint("api", __name__)
api = Api(api_bp)
Expand Down Expand Up @@ -46,10 +48,14 @@ class ProtectedResource(Resource):
method_decorators = [verify_access]

def get_roller_status(self, blind):
response = requests.get("http://{ip}/roller/0".format(ip=blind.get("ip")))
try:
response = requests.get("http://{ip}/roller/0".format(ip=blind.get("ip")))
except Exception as e:
logger.exception(e)
return {}
if not response.ok:
app.logger.error(
'Unable to retrieve status of Blind "{id}" ({ip}): {reason}'.format(
'Unable to retrieve status of Blind "{}" ({}): {}'.format(
blind.get("id"), blind.get("ip"), response.reason
)
)
Expand All @@ -62,7 +68,6 @@ def get(self):
res = []
for blind in app.config.get("BLINDS", []):
blind_infos = blind.copy()
response = requests.get("http://{ip}/roller/0".format(ip=blind.get("ip")))
roller_status = self.get_roller_status(blind=blind)
if roller_status:
blind_infos["action"] = roller_status.get("state")
Expand Down

0 comments on commit 3fff1c9

Please sign in to comment.