From f1ce9fb4550e1653118244b8682c731d55bbfcb9 Mon Sep 17 00:00:00 2001 From: Marius Alwan Meyer Date: Fri, 29 Sep 2023 13:37:47 +0200 Subject: [PATCH] Overhaul error handling --- openweather.py | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/openweather.py b/openweather.py index a9a24dc..ec2c976 100755 --- a/openweather.py +++ b/openweather.py @@ -15,30 +15,31 @@ response = requests.get(complete_url) data = response.json() -if response.status_code == 200: +print() +match response.status_code: + case 200: + info_main = data["main"] + info_weather = data["weather"] + current_temperature = info_main["temp"] + current_pressure = info_main["pressure"] + current_humidity = info_main["humidity"] + weather_description = info_weather[0]["description"] - info_main = data["main"] - info_weather = data["weather"] - current_temperature = info_main["temp"] - current_pressure = info_main["pressure"] - current_humidity = info_main["humidity"] - weather_description = info_weather[0]["description"] + print(Columns([ + Panel(f"[blue]{round(current_temperature - 273.15, 2)}°C", + title="[bold]Temperature[/bold]"), - print() - print(Columns([ - Panel(f"[blue]{round(current_temperature - 273.15, 2)}°C", - title="[bold]Temperature[/bold]"), + Panel(f"[blue]{current_pressure}hPa", + title="[bold]Atmospheric Pressure[/bold]"), - Panel(f"[blue]{current_pressure}hPa", - title="[bold]Atmospheric Pressure[/bold]"), + Panel(f"[blue]{current_humidity}%", + title="[bold]Humidity[/bold]"), + + Panel(f"[blue]{weather_description}", + title="[bold]Description[/bold]"), + ])) - Panel(f"[blue]{current_humidity}%", - title="[bold]Humidity[/bold]"), - - Panel(f"[blue]{weather_description}", - title="[bold]Description[/bold]"), - ])) - -else: - print(f"Fehler: {response.status_code}") - print(response.text) + case _: + print(Panel(data["message"], + title=f"[bold red]Error: {response.status_code}", + expand=False))