Annotate refreshment stops, improved logging and error handling.
This commit is contained in:
parent
7d3b208e5c
commit
52d41490f3
5 changed files with 195 additions and 25 deletions
|
@ -1,7 +1,6 @@
|
|||
package rwgps
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
@ -15,6 +14,14 @@ func (e *ErrNotFound) Error() string {
|
|||
return fmt.Sprintf("RideWithGPS track %d not found", e.RouteId)
|
||||
}
|
||||
|
||||
type ErrNotPublic struct {
|
||||
RouteId int
|
||||
}
|
||||
|
||||
func (e *ErrNotPublic) Error() string {
|
||||
return fmt.Sprintf("RideWithGPS track %d is not public", 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)
|
||||
|
@ -22,16 +29,18 @@ func FetchTrack(routeId int) ([]byte, error) {
|
|||
return nil, fmt.Errorf("error getting %s: %v", url, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return nil, &ErrNotFound{routeId}
|
||||
}
|
||||
if resp.StatusCode == http.StatusForbidden {
|
||||
return nil, &ErrNotPublic{routeId}
|
||||
}
|
||||
return nil, fmt.Errorf("error retrieving route %d: %s", routeId, resp.Status)
|
||||
}
|
||||
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)"))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue