1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: properly handle error responses during wakatime import

This commit is contained in:
Ferdinand Mütsch 2023-04-21 09:43:10 +02:00
parent df25183035
commit a6915a187a

View File

@ -146,17 +146,21 @@ func (w *WakatimeHeartbeatImporter) fetchHeartbeats(day string, baseUrl string)
q.Add("date", day) q.Add("date", day)
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
var empty []*wakatime.HeartbeatEntry
res, err := w.httpClient.Do(w.withHeaders(req)) res, err := w.httpClient.Do(w.withHeaders(req))
if err != nil { if err != nil {
return nil, err return empty, err
} else if res.StatusCode == 402 {
return empty, nil // date outside free plan range -> return empty data, but do not throw error
} else if res.StatusCode >= 400 { } else if res.StatusCode >= 400 {
return nil, errors.New(fmt.Sprintf("got status %d from wakatime api", res.StatusCode)) return empty, errors.New(fmt.Sprintf("got status %d from wakatime api", res.StatusCode))
} }
defer res.Body.Close() defer res.Body.Close()
var heartbeatsData wakatime.HeartbeatsViewModel var heartbeatsData wakatime.HeartbeatsViewModel
if err := json.NewDecoder(res.Body).Decode(&heartbeatsData); err != nil { if err := json.NewDecoder(res.Body).Decode(&heartbeatsData); err != nil {
return nil, err return empty, err
} }
return heartbeatsData.Data, nil return heartbeatsData.Data, nil