refactor: move error handling

This commit is contained in:
Slug-Boi
2025-04-09 21:01:00 +02:00
parent 4696f58bf5
commit 81adc7e2c3
+13 -15
View File
@@ -49,18 +49,14 @@ func FetchGithubProfile(username string) User {
check := checkGHCLI() check := checkGHCLI()
var err error
if check { if check {
// If the gh command line tool is installed, use it to fetch the github profile // If the gh command line tool is installed, use it to fetch the github profile
fmt.Println("Using gh-cli to fetch github profile") fmt.Println("Using gh-cli to fetch github profile")
data := useGHCLI(username) data := useGHCLI(username)
err := json.Unmarshal(data, &profile) err = json.Unmarshal(data, &profile)
if err != nil {
panic(fmt.Sprint("Error parsing github profile: ", err))
}
if profile.Name == "" {
panic(fmt.Sprint("Error: No name found in github profile something went wrong whilst fetching using gh-cli \n(output from cmd:)", string(data)))
}
} else { } else {
fmt.Println("Using http request to fetch github profile") fmt.Println("Using http request to fetch github profile")
@@ -74,17 +70,19 @@ func FetchGithubProfile(username string) User {
defer resp.Body.Close() defer resp.Body.Close()
// Parse the response and create a user // Parse the response and create a user
err = json.NewDecoder(resp.Body).Decode(&profile) if err = json.NewDecoder(resp.Body).Decode(&profile); err != nil {
if err != nil { panic(fmt.Sprint("Error decoding github profile: ", err))
panic(fmt.Sprint("Error parsing github profile: ", err))
}
if profile.Name == "" {
panic(fmt.Sprintf("Error: No name found in github profile something went wrong whilst fetching \n(http response): %s", resp.Status))
} }
} }
// Check error
if err != nil {
panic(fmt.Sprint("Error parsing github profile: ", err))
}
// Check if the profile has a name
if profile.Name == "" {
panic(fmt.Sprint("Error: No name found in github profile something went wrong whilst fetching the profile: ", err))
}
// Create a user with the github profile // Create a user with the github profile
return User{ return User{