Handle bad status code in schedule response
continuous-integration/drone/push Build is failing Details

This commit is contained in:
nemunaire 2023-01-16 02:13:11 +01:00
parent 27b5cb5d11
commit 286d1af908
1 changed files with 14 additions and 3 deletions

View File

@ -3,6 +3,8 @@ package api
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"sort"
@ -33,8 +35,9 @@ type IDFMRealTimeData struct {
type IDFMRealTime struct {
NextDepartures struct {
StatusCode int `json:"statusCode"`
Data []IDFMRealTimeData `json:"data"`
StatusCode int `json:"statusCode"`
ErrorMessage string `json:"errorMessage"`
Data []IDFMRealTimeData `json:"data"`
} `json:"nextDepartures"`
CrowdsourcingReports struct {
congestions []struct {
@ -114,6 +117,8 @@ func getRealTime(code, station string) (*IDFMRealTime, error) {
defer res.Body.Close()
if res.StatusCode >= 400 {
v, _ := io.ReadAll(res.Body)
log.Println("Schedule not found: ", string(v))
return nil, fmt.Errorf("Schedule not found")
}
@ -124,13 +129,18 @@ func getRealTime(code, station string) (*IDFMRealTime, error) {
return nil, err
}
if schedules.NextDepartures.StatusCode >= 400 {
log.Println("Schedule not found: ", schedules)
return nil, fmt.Errorf("Schedule not found: %s", schedules.NextDepartures.ErrorMessage)
}
return &schedules, nil
}
func declareSchedulesRoutes(router *gin.RouterGroup) {
router.GET("/schedules/:type/:code/:station/:way", func(c *gin.Context) {
t := convertLineType(string(c.Param("type")))
code := string(c.Param("code"))
code := convertCode(t, string(c.Param("code")))
station := string(c.Param("station"))
way := string(c.Param("way"))
@ -154,6 +164,7 @@ func declareSchedulesRoutes(router *gin.RouterGroup) {
}
}
log.Println("search", code, station)
schedules, err := getRealTime(code, station)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})