From fdc574f5f290edbb7ee1b27cfd8f359d3aaa5795 Mon Sep 17 00:00:00 2001 From: Alex Lovell-Troy Date: Mon, 1 Jul 2024 13:53:32 -0400 Subject: [PATCH] Improved Error handling without panics --- cmd/crawl.go | 3 ++- pkg/crawler/main.go | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/crawl.go b/cmd/crawl.go index 4d086ec..f61ae4d 100644 --- a/cmd/crawl.go +++ b/cmd/crawl.go @@ -3,6 +3,7 @@ package cmd import ( "encoding/json" "fmt" + "log" "net/url" "github.com/OpenCHAMI/magellan/pkg/crawler" @@ -31,7 +32,7 @@ var crawlCmd = &cobra.Command{ Insecure: cmd.Flag("insecure").Value.String() == "true", }) if err != nil { - panic(err) + log.Fatalf("Error crawling BMC: %v", err) } // Marshal the inventory details to JSON jsonData, err := json.MarshalIndent(systems, "", " ") diff --git a/pkg/crawler/main.go b/pkg/crawler/main.go index d2d5b64..d7869ed 100644 --- a/pkg/crawler/main.go +++ b/pkg/crawler/main.go @@ -2,6 +2,7 @@ package crawler import ( "fmt" + "strings" "github.com/rs/zerolog/log" "github.com/stmcginnis/gofish" @@ -56,6 +57,12 @@ func CrawlBMC(config CrawlerConfig) ([]InventoryDetail, error) { BasicAuth: true, }) 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.Err(err) event.Msg("failed to connect to BMC")