diff options
author | Julien Dessaux | 2021-05-04 16:19:42 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-05-04 16:47:07 +0200 |
commit | d2658b5c0c7bd8c6214a8e34582cdba23da8ce0d (patch) | |
tree | 5f55025c86f7ebbb0932bc94e2e1415cbfe8f040 /pkg/navitia_api_client/departures.go | |
parent | Reworked the navitia_api_client to be mockable (diff) | |
download | trains-d2658b5c0c7bd8c6214a8e34582cdba23da8ce0d.tar.gz trains-d2658b5c0c7bd8c6214a8e34582cdba23da8ce0d.tar.bz2 trains-d2658b5c0c7bd8c6214a8e34582cdba23da8ce0d.zip |
Improved navitia api error handling and testing
Diffstat (limited to '')
-rw-r--r-- | pkg/navitia_api_client/departures.go | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/pkg/navitia_api_client/departures.go b/pkg/navitia_api_client/departures.go index 7447cdc..50ebe17 100644 --- a/pkg/navitia_api_client/departures.go +++ b/pkg/navitia_api_client/departures.go @@ -57,27 +57,32 @@ func (c *NavitiaClient) GetDepartures(trainStop string) (departures []model.Depa } req, err := http.NewRequest("GET", request, nil) if err != nil { - return nil, err + return nil, newHttpClientError("http.NewRequest error", err) } resp, err := c.httpClient.Do(req) if err != nil { - return nil, err + return nil, newHttpClientError("httpClient.Do error", err) } defer resp.Body.Close() - var data DeparturesResponse - if err = json.NewDecoder(resp.Body).Decode(&data); err != nil { - return nil, err - } - for i := 0; i < len(data.Departures); i++ { - t, err := time.Parse("20060102T150405", data.Departures[i].StopDateTime.ArrivalDateTime) - if err != nil { // TODO fail better - panic(err) + if resp.StatusCode == http.StatusOK { + var data DeparturesResponse + if err = json.NewDecoder(resp.Body).Decode(&data); err != nil { + return nil, newJsonDecodeError("GetDepartures "+trainStop, err) } - departures = append(departures, model.Departure{data.Departures[i].DisplayInformations.Direction, t.Format("Mon, 02 Jan 2006 15:04:05")}) - } - c.cache[request] = cachedResult{ - ts: start, - result: departures, + // TODO test for no json error + for i := 0; i < len(data.Departures); i++ { + t, err := time.Parse("20060102T150405", data.Departures[i].StopDateTime.ArrivalDateTime) + if err != nil { + return nil, newDateParsingError(data.Departures[i].StopDateTime.ArrivalDateTime, err) + } + departures = append(departures, model.Departure{data.Departures[i].DisplayInformations.Direction, t.Format("Mon, 02 Jan 2006 15:04:05")}) + } + c.cache[request] = cachedResult{ + ts: start, + result: departures, + } + } else { + err = newApiError(resp.StatusCode, "GetDepartures "+trainStop) } return } |