Improved Error handling without panics

This commit is contained in:
Alex Lovell-Troy 2024-07-01 13:53:32 -04:00
parent 3386690f17
commit fdc574f5f2
No known key found for this signature in database
2 changed files with 9 additions and 1 deletions

View file

@ -3,6 +3,7 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/url" "net/url"
"github.com/OpenCHAMI/magellan/pkg/crawler" "github.com/OpenCHAMI/magellan/pkg/crawler"
@ -31,7 +32,7 @@ var crawlCmd = &cobra.Command{
Insecure: cmd.Flag("insecure").Value.String() == "true", Insecure: cmd.Flag("insecure").Value.String() == "true",
}) })
if err != nil { if err != nil {
panic(err) log.Fatalf("Error crawling BMC: %v", err)
} }
// Marshal the inventory details to JSON // Marshal the inventory details to JSON
jsonData, err := json.MarshalIndent(systems, "", " ") jsonData, err := json.MarshalIndent(systems, "", " ")

View file

@ -2,6 +2,7 @@ package crawler
import ( import (
"fmt" "fmt"
"strings"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/stmcginnis/gofish" "github.com/stmcginnis/gofish"
@ -56,6 +57,12 @@ func CrawlBMC(config CrawlerConfig) ([]InventoryDetail, error) {
BasicAuth: true, BasicAuth: true,
}) })
if err != nil { if err != nil {
if strings.HasPrefix(err.Error(), "404:") {
err = fmt.Errorf("no ServiceRoot found. This is probably not a BMC: %s", config.URI)
}
if strings.HasPrefix(err.Error(), "401:") {
err = fmt.Errorf("authentication failed. Check your username and password: %s", config.URI)
}
event := log.Error() event := log.Error()
event.Err(err) event.Err(err)
event.Msg("failed to connect to BMC") event.Msg("failed to connect to BMC")