ncdulo/weather_cli

[BUG] Program fails when data missing from API call

Closed this issue · 1 comments

Describe the bug
When running the current command to query current weather conditions from OpenWeatherMap it is possible for the program to fail with a KeyError when pieces of the data being expected are not present. The missing data is due to OpenWeatherMap not having data for that parameter to return. That is a perfectly normal condition and we should exit gracefully when it is encountered.

To Reproduce
This bug may not appear every time, under every condition. Takes a bit of playing around with different locations, and different flags to find a location where not all data is available. The command I have been able to consistently cause this KeyError with is:

weather current Dubai --wind

Expected behavior
Rather than not handling the error and allowing Python to throw a Traceback all over the console, we should catch the error and handle it accordingly. This may include simply omitting the data from the output, or including a warning about the parameter not being returned from the API. The user should not have to dig through a Traceback simply because OpenWeatherMap does not know the humidity, or wind speed, etc, for a given location.

Output

[$] [1] <git:(master)> .env/bin/weather current Dubai --wind
Traceback (most recent call last):
  File ".env/bin/weather", line 11, in <module>
    load_entry_point('weather', 'console_scripts', 'weather')()
  File "/home/ncdulo/dev/projects/weather_cli/.env/lib64/python3.6/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/ncdulo/dev/projects/weather_cli/.env/lib64/python3.6/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/ncdulo/dev/projects/weather_cli/.env/lib64/python3.6/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/ncdulo/dev/projects/weather_cli/.env/lib64/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/ncdulo/dev/projects/weather_cli/.env/lib64/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/ncdulo/dev/projects/weather_cli/.env/lib64/python3.6/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/ncdulo/dev/projects/weather_cli/cli.py", line 166, in current
    wind_cardinal = degrees_to_cardinal(weather['wind']['deg'])
KeyError: 'deg'

Desktop (please complete the following information):

  • OS/Distribution: Gentoo Linux 17.1 Desktop
  • Python 3.6.10
  • requests-2.23.0
  • click-7.1.1
  • Python virtual environment? Yes

Additional context
A similar bug will manifest when the location name is not found by OpenWeatherMap. If the location name is not found, the request will still return perfectly fine. However there will be no data for us to use, and we fail with relatively the same KeyError. I am rolling this into a single bug report as error checking and handling falls under the same tree.

Probably should have done a proper PR so I could link this issue to it. But I didn't, and I did not include any closing comments into the commit message, so here we are.

This issue has been resolve as per merge commit cb91ac5. I have tested to the best of my ability using the API itself. However we really should write unit tests to properly test this. For now, this issue is resolved & closed.