Skip to content
This repository was archived by the owner on Mar 22, 2025. It is now read-only.

Commit 79cab32

Browse files
committed
added error handling in getApiPricedata
1 parent 64b925d commit 79cab32

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Comfortstat/things.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ var err_statuscode error = fmt.Errorf("bad status code")
7979
// This function fetches the current electricity price from "https://www.elprisetjustnu.se/elpris-api", then prosess it and updates globalPrice
8080
func getAPIPriceData(apiURL string) error {
8181
//Validate the URL//
82-
parsedURL, err := url.Parse(apiURL) // ensures the string is a valid UTL, .schema and .Host checks prevent emty or altered URL
82+
parsedURL, err := url.Parse(apiURL) // ensures the string is a valid URL, .schema and .Host checks prevent emty or altered URL
8383
if err != nil || parsedURL.Scheme == "" || parsedURL.Host == "" {
84-
return errors.New("invalid URL")
84+
return errors.New("The URL is invalid")
8585
}
8686
// end of validating the URL//
8787
res, err := http.Get(parsedURL.String())
@@ -94,9 +94,14 @@ func getAPIPriceData(apiURL string) error {
9494
return err
9595
}
9696

97-
var data []GlobalPriceData // Create a list to hold the gateway json
97+
var data []GlobalPriceData // Create a list to hold the data json
9898
err = json.Unmarshal(body, &data) // "unpack" body from []byte to []GlobalPriceData, save errors
99-
res.Body.Close() // defer res.Body.Close()
99+
/*
100+
if err != nil {
101+
return err
102+
}
103+
*/
104+
defer res.Body.Close() // defer res.Body.Close()
100105

101106
if res.StatusCode > 299 {
102107
return err_statuscode

0 commit comments

Comments
 (0)