From: Sven Höxter Date: Tue, 24 Aug 2021 07:50:19 +0000 (+0200) Subject: Catch requests exceptions X-Git-Url: https://git.sven.stormbind.net/?a=commitdiff_plain;h=5d454e4a51fc630855d24f868717e51cf2c66857;p=sven%2Fscripts.git Catch requests exceptions --- 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)