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,15 +31,16 @@ 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__':
try:
response = requests.get('https://wttr.in/?format=j1') response = requests.get('https://wttr.in/?format=j1')
# Due to polybar being weird everything goes to stdout
if response.status_code == 200: if response.status_code == 200:
try: try:
report = Report(response.json()) report = Report(response.json())
@ -51,3 +52,5 @@ if __name__ == '__main__':
print('Error caught', e, file=sys.stderr) print('Error caught', e, file=sys.stderr)
else: else:
print('Unable to fetch data from wttr.in', file=sys.stderr) print('Unable to fetch data from wttr.in', file=sys.stderr)
except:
print('Unable to hit wttr API')