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): 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}" return f"It's {self.temp_c}° @ {self.hum}% humidity Status: {self.desc}"
if __name__ == '__main__': if __name__ == '__main__':
response = requests.get('https://wttr.in/?format=j1') try:
response = requests.get('https://wttr.in/?format=j1')
if response.status_code == 200: # Due to polybar being weird everything goes to stdout
try: if response.status_code == 200:
report = Report(response.json()) try:
print(report) report = Report(response.json())
except ReportErr as re: print(report)
print(re, file=sys.stderr) except ReportErr as re:
except Exception as e: print(re, file=sys.stderr)
print('unable to decode response payload', file=sys.stderr) except Exception as e:
print('Error caught', e, file=sys.stderr) print('unable to decode response payload', file=sys.stderr)
else: print('Error caught', e, file=sys.stderr)
print('Unable to fetch data from wttr.in', file=sys.stderr) else:
print('Unable to fetch data from wttr.in', file=sys.stderr)
except:
print('Unable to hit wttr API')