Improve error handling

This commit is contained in:
raul 2024-05-21 10:38:28 +02:00
parent faad6a72fb
commit 843cf7ab94
2 changed files with 10 additions and 10 deletions

View File

@ -77,7 +77,7 @@ func getJSON(codPostal string) (s string, err error) {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://www.aemet.es/xml/municipios/localidad_"+codPostal+".xml", nil)
if err != nil {
e := fmt.Errorf("Error occurred pulling data: %v\n", err)
e := fmt.Errorf("Error occurred creating GET request: %v\n", err)
return "", e
}
req.Header.Set("User-Agent", "AEMET-Client/1.0 (https://git.bulgariu.xyz/raul/aemet)")
@ -85,7 +85,7 @@ func getJSON(codPostal string) (s string, err error) {
resp, err := client.Do(req)
if err != nil {
e := fmt.Errorf("Error occurred pulling data: %v\n", err)
e := fmt.Errorf("Error occurred executing GET request: %v\n", err)
return "", e
}

View File

@ -34,14 +34,6 @@ func returnWeather(c *gin.Context) {
codPostal := c.Param("localidad")
numDias := c.Param("dia")
if numDias != "" {
n, err = strconv.Atoi(numDias)
if err != nil {
c.String(http.StatusNotAcceptable, "Invalid day identifier")
return
}
}
isAvailable := false
for k := range localidades {
if codPostal == k {
@ -67,6 +59,14 @@ func returnWeather(c *gin.Context) {
return
}
if numDias != "" {
n, err = strconv.Atoi(numDias)
if err != nil || n > len(aemetRequest.Base.Prediccion.Dia)-1 || n < 0 {
c.String(http.StatusNotAcceptable, "Invalid day identifier")
return
}
}
if numDias != "" {
c.IndentedJSON(http.StatusOK, aemetRequest.Base.Prediccion.Dia[n])
} else {