From 62b43e1cd2c580012ff594aac9cc0e41f4689774 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Wed, 10 Jan 2024 12:53:36 +1300 Subject: [PATCH] Better error handling --- internal/handler/insightsFactsParserFilter.go | 15 ++++++++------- internal/handler/insightsParserFilter.go | 6 ++---- internal/handler/main.go | 11 +++++++---- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/internal/handler/insightsFactsParserFilter.go b/internal/handler/insightsFactsParserFilter.go index 667569c..154d95a 100644 --- a/internal/handler/insightsFactsParserFilter.go +++ b/internal/handler/insightsFactsParserFilter.go @@ -28,7 +28,10 @@ func processFactsInsightsData(h *Messaging, insights InsightsData, v string, api slog.Error("Error reading insights data", "Error", err) } - facts := processFactsFromJSON(logger, res, source) + facts, err := processFactsFromJSON(logger, res, source) + if err != nil { + return nil, "", err + } facts, err = KeyFactsFilter(facts) if err != nil { return nil, "", err @@ -38,7 +41,6 @@ func processFactsInsightsData(h *Messaging, insights InsightsData, v string, api return nil, "", fmt.Errorf("no facts to process") } - //log.Printf("Successfully processed %d fact(s), for '%s:%s', from source '%s'", len(facts), resource.Project, resource.Environment, source) logger.Info("Successfully processed facts", "number", len(facts)) return facts, source, nil @@ -46,18 +48,17 @@ func processFactsInsightsData(h *Messaging, insights InsightsData, v string, api return nil, "", nil } -func processFactsFromJSON(logger *slog.Logger, facts []byte, source string) []LagoonFact { +func processFactsFromJSON(logger *slog.Logger, facts []byte, source string) ([]LagoonFact, error) { var factsInput []LagoonFact var factsPayload FactsPayload err := json.Unmarshal(facts, &factsPayload) if err != nil { - logger.Error(err.Error()) - return nil + return factsInput, err } if len(factsPayload.Facts) == 0 { - return factsInput + return factsInput, nil } var filteredFacts []LagoonFact @@ -85,7 +86,7 @@ func processFactsFromJSON(logger *slog.Logger, facts []byte, source string) []La fact, _ = ProcessLagoonFactAgainstRegisteredFilters(fact, f) factsInput = append(factsInput, fact) } - return factsInput + return factsInput, nil } func init() { diff --git a/internal/handler/insightsParserFilter.go b/internal/handler/insightsParserFilter.go index 32689d9..1c54e93 100644 --- a/internal/handler/insightsParserFilter.go +++ b/internal/handler/insightsParserFilter.go @@ -55,7 +55,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC decoder := cdx.NewBOMDecoder(bytes.NewReader(b), cdx.BOMFileFormatJSON) if err = decoder.Decode(bom); err != nil { logger.Error(err.Error()) - return nil, "", mErr + return nil, "", err } } @@ -131,9 +131,7 @@ func processFactsFromSBOM(logger *slog.Logger, facts *[]cdx.Component, environme KeyFact: false, Type: FactTypeText, } - //if EnableDebug { - // log.Println("[DEBUG] processing fact name " + f.Name) - //} + logger.Debug("Processing fact", "Name", f.Name, "Value", f.Version, diff --git a/internal/handler/main.go b/internal/handler/main.go index db7bf2a..6967440 100644 --- a/internal/handler/main.go +++ b/internal/handler/main.go @@ -302,9 +302,10 @@ func parserFilterLoopForBinaryPayloads(insights InsightsData, p string, h *Messa result, source, err := filter(h, insights, p, apiClient, resource) if err != nil { slog.Error("Error running filter", "error", err.Error()) + continue } - processResultset(result, err, h, apiClient, resource, source) + processResultset(result, h, apiClient, resource, source) } } @@ -316,19 +317,21 @@ func parserFilterLoopForPayloads(insights InsightsData, p PayloadInput, h *Messa json, err := json.Marshal(p) if err != nil { slog.Error("Error marshalling data", "error", err.Error()) + continue } result, source, err = filter(h, insights, fmt.Sprintf("%s", json), apiClient, resource) if err != nil { slog.Error("Error Filtering payload", "error", err.Error()) + continue } - processResultset(result, err, h, apiClient, resource, source) + processResultset(result, h, apiClient, resource, source) } } // processResultset will send results as facts to the lagoon api after processing via a parser filter -func processResultset(result []interface{}, err error, h *Messaging, apiClient graphql.Client, resource ResourceDestination, source string) { +func processResultset(result []interface{}, h *Messaging, apiClient graphql.Client, resource ResourceDestination, source string) { project, environment, apiErr := determineResourceFromLagoonAPI(apiClient, resource) if apiErr != nil { log.Println(apiErr) @@ -344,7 +347,7 @@ func processResultset(result []interface{}, err error, h *Messaging, apiClient g for _, r := range result { if fact, ok := r.(LagoonFact); ok { // Handle single fact - err = h.sendFactsToLagoonAPI([]LagoonFact{fact}, apiClient, resource, source) + err := h.sendFactsToLagoonAPI([]LagoonFact{fact}, apiClient, resource, source) if err != nil { slog.Error("Error sending facts to Lagoon API", "error", err.Error()) }