From 584e30ca7139ca87dd639de2a6a4a192ca8a95cf Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 6 Dec 2022 09:25:14 -0800 Subject: [PATCH] Wrapping API call in try/except --- weather-fetch/fetch.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/weather-fetch/fetch.py b/weather-fetch/fetch.py index f2c2180..678d203 100755 --- a/weather-fetch/fetch.py +++ b/weather-fetch/fetch.py @@ -31,23 +31,26 @@ class Report: def __str__(self): ''' - Basically just a pretty print that we use to + Basically just a pretty print that we use to show on the bar ''' return f"It's {self.temp_c}° @ {self.hum}% humidity Status: {self.desc}" if __name__ == '__main__': - response = requests.get('https://wttr.in/?format=j1') - - if response.status_code == 200: - try: - report = Report(response.json()) - print(report) - except ReportErr as re: - print(re, file=sys.stderr) - except Exception as e: - print('unable to decode response payload', file=sys.stderr) - print('Error caught', e, file=sys.stderr) - else: - print('Unable to fetch data from wttr.in', file=sys.stderr) + try: + response = requests.get('https://wttr.in/?format=j1') + # Due to polybar being weird everything goes to stdout + if response.status_code == 200: + try: + report = Report(response.json()) + print(report) + except ReportErr as re: + print(re, file=sys.stderr) + except Exception as e: + print('unable to decode response payload', file=sys.stderr) + print('Error caught', e, file=sys.stderr) + else: + print('Unable to fetch data from wttr.in', file=sys.stderr) + except: + print('Unable to hit wttr API')