Wrapping API call in try/except

This commit is contained in:
shockrah 2022-12-06 09:25:14 -08:00
parent 736f5b9a7c
commit 584e30ca71

View File

@ -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')