From 5d454e4a51fc630855d24f868717e51cf2c66857 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20H=C3=B6xter?= Date: Tue, 24 Aug 2021 09:50:19 +0200 Subject: [PATCH] Catch requests exceptions --- home/portalmonitor.py | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/home/portalmonitor.py b/home/portalmonitor.py index 13a7b98..12a088e 100755 --- a/home/portalmonitor.py +++ b/home/portalmonitor.py @@ -39,22 +39,32 @@ def isDaylight(lat, lon, toleranceSeconds): def getCurrentPower(userName, password, stationId): with requests.Session() as s: - r = s.post('https://www.envertecportal.com/apiaccount/login', - data={ - 'userName': userName, - 'pwd': password - }, - timeout=(10, 30)) - - r = s.post('https://www.envertecportal.com/ApiStations/getStationInfo', - data={ - 'stationId': stationId - }, - timeout=(10, 60)).json() - power = r['Data']['Power'] - - r = s.post('https://www.envertecportal.com/apiAccount/Logout', - timeout=(10, 30)) + try: + r = s.post('https://www.envertecportal.com/apiaccount/login', + data={ + 'userName': userName, + 'pwd': password + }, + timeout=(10, 30)) + except requests.exceptions.RequestException as e: + raise SystemExit(e) + + try: + r = s.post( + 'https://www.envertecportal.com/ApiStations/getStationInfo', + data={ + 'stationId': stationId + }, + timeout=(10, 60)).json() + power = r['Data']['Power'] + except requests.exceptions.RequestException as e: + raise SystemExit(e) + + try: + r = s.post('https://www.envertecportal.com/apiAccount/Logout', + timeout=(10, 30)) + except requests.exceptions.RequestException as e: + raise SystemExit(e) return float(power) -- 2.39.5