leftshift / python_mvg_api

Yet another library for fetching MVG departures.
https://pypi.org/project/mvg-api/
MIT License
65 stars 18 forks source link

Uncatched error when get_route is called on invalid dates #20

Closed jhennrich closed 4 years ago

jhennrich commented 4 years ago

When calling get_route with an invalid date (I tried several dates, all before 2019-12-15 were invalid for me) the api crashes with:

in get_route(start, dest, time, arrival_time, max_walk_time_to_start, max_walk_time_to_dest, change_limit)
    259     url = routing_url + options_url
    260     results = _perform_api_request(url)
--> 261     for connection in results["connectionList"]:
    262         connection["departure_datetime"] = \
    263             _convert_time(connection["departure"])

KeyError: 'connectionList'

This error occurs, because the dict that is returned from _perform_api_request looks like this: {'type': 'http://apps.mvg-fahrinfo.de/v12/rest/2.0/error/routing_request_invalid_date', 'title': 'invalid date maybe after time table change'}

Fix: there should be a check after _perform_api_request if the returned dict contains actual content or an error message. I do not know by which rule it is determined, whether a date is correct.

Best regards ~

leftshift commented 4 years ago

Fixed in 384a5a7, thanks :)

Now, an exception is thrown when perform_api_request gets a response with an non-ok status code. (this should have always been the case and I'm surprised you're the first person to notice or complain)