Logging and better handling of RideWithGPS route not found.

This commit is contained in:
Ray Miller 2020-04-20 12:43:41 +01:00
parent a51a3634bd
commit 7d3b208e5c
2 changed files with 51 additions and 19 deletions

37
pkg/rwgps/rwgps.go Normal file
View file

@ -0,0 +1,37 @@
package rwgps
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
type ErrNotFound struct {
RouteId int
}
func (e *ErrNotFound) Error() string {
return fmt.Sprintf("RideWithGPS track %d not found", e.RouteId)
}
func FetchTrack(routeId int) ([]byte, error) {
url := fmt.Sprintf("https://ridewithgps.com/routes/%d.gpx?sub_format=track", routeId)
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("error getting %s: %v", url, err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response from %s: %v", url, err)
}
if IsNotFound(data) {
return nil, &ErrNotFound{routeId}
}
return data, nil
}
func IsNotFound(data []byte) bool {
return bytes.HasPrefix(data, []byte("<!DOCTYPE html>")) && bytes.Contains(data, []byte("Error (404 not found)"))
}