ClemensGruber / climart-gptree

MIT License
2 stars 1 forks source link

Catch and handle API errors, e.g. no weather data (ignore, message to user) #21

Open ClemensGruber opened 7 months ago

ClemensGruber commented 7 months ago

We should output something like the traceback / debug information below but intercept it and ignore i.g. weather data for this promt call in case of missing data.

Current problem is: Weatherstation is down and

https://weather.hiveeyes.org/api/climart/zku/weatherstation/main/data.json?from=now-168h&to=now

provides

# 404 Not Found
#
# No data for query expression "SELECT * FROM weatherstation_main_sensors WHERE time >= '2024-02-06 21:50:39.203' AND time <= '2024-02-13 21:50:39.203';"
# Please recognize absolute datetimes are expected to be in ISO 8601 format. Default is UTC, optionally specify an appropriate timezone offset.
#
# Examples:
#
#   ?from=2016-06-25T22:00:00.000Z
#   ?from=2016-06-26T00:00:00.000%2B02:00    (%2B is "+" urlencoded)
#   ?from=now-4h&to=now-2h
#   ?from=now-8d5h3m&to=now-6d

GPTree crashed with this output

Traceback (most recent call last):

  File "/home/climart/gptree/main.py", line 152, in <module>
    main()
  File "/home/climart/gptree/main.py", line 132, in main
    system_context=add_current_context(persona["prompt"])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/utils/weather.py", line 8, in add_current_context
    df = pd.read_json("https://weather.hiveeyes.org/api/climart/zku/weatherstation/main/data.json?from=now-168h&to=now")
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/pandas/io/json/_json.py", line 780, in read_json
    json_reader = JsonReader(
                  ^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/pandas/io/json/_json.py", line 893, in __init__
    data = self._get_data_from_filepath(filepath_or_buffer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/pandas/io/json/_json.py", line 933, in _get_data_from_filepath
    self.handles = get_handle(
                   ^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/pandas/io/common.py", line 718, in get_handle
    ioargs = _get_filepath_or_buffer(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/pandas/io/common.py", line 372, in _get_filepath_or_buffer
    with urlopen(req_info) as req:
         ^^^^^^^^^^^^^^^^^
  File "/home/climart/gptree/gptree/lib/python3.11/site-packages/pandas/io/common.py", line 274, in urlopen
    return urllib.request.urlopen(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 525, in open
    response = meth(req, response)
               ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 634, in http_response
    response = self.parent.error(
               ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 563, in error
    return self._call_chain(*args)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 496, in _call_chain
    result = func(*args)
             ^^^^^^^^^^^
  File "/usr/lib/python3.11/urllib/request.py", line 643, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found