Skip to content
Snippets Groups Projects
Commit e2586de8 authored by Torgrim's avatar Torgrim
Browse files

Updated Error Handling to be more Precise

parent b04d78ff
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ package _func
import (
"encoding/json"
"errors"
"fmt"
"globeboard/internal/utils/constants/External"
"globeboard/internal/utils/structs"
......@@ -283,5 +284,5 @@ func getExchangeRateList(isocode string) (map[string]float64, error) {
return rates, nil
}
return nil, fmt.Errorf("no currency currency found")
return nil, errors.New("no currency currency found")
}
......@@ -13,13 +13,7 @@ import (
"time"
)
// CountryInfo represents the necessary information about a country, focusing on its common name and cca2 code.
type CountryInfo struct {
CommonName string `json:"commonName"`
ISOCode string `json:"isoCode"`
}
// GetSupportedCountries fetches countries with their common names and cca2 codes.
// getSupportedCountries fetches countries with their common names and cca2 codes.
func getSupportedCountries() (map[string]string, error) {
url := fmt.Sprintf("%sall?fields=name,cca2", External.CountriesAPI)
var responseData []struct {
......@@ -32,18 +26,18 @@ func getSupportedCountries() (map[string]string, error) {
client := &http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %s", err)
return nil, fmt.Errorf("error creating request: %v", err)
}
req.Header.Add("content-type", "application/json")
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error issuing request: %s", err)
return nil, fmt.Errorf("error issuing request: %v", err)
}
err = json.NewDecoder(res.Body).Decode(&responseData)
if err != nil {
return nil, fmt.Errorf("error decoding JSON: %s", err)
return nil, fmt.Errorf("error decoding JSON: %v", err)
}
countriesMap := make(map[string]string)
......
......@@ -19,7 +19,7 @@ func getEndpointStatus(endpointURL string) string {
r, err := http.NewRequest(http.MethodGet, endpointURL, nil)
if err != nil {
// Log and handle the error if request creation fails.
err := fmt.Errorf("error in creating request: %s", err.Error())
err := fmt.Errorf("error in creating request: %v", err)
log.Println(err)
}
......@@ -34,7 +34,7 @@ func getEndpointStatus(endpointURL string) string {
res, err := client.Do(r)
if err != nil {
// Log and handle the error if request execution fails.
err := fmt.Errorf("error in response: %s", err.Error())
err := fmt.Errorf("error in response: %v", err)
log.Println(err)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment