Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Jan 9, 2024
1 parent 144d821 commit 62b43e1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
15 changes: 8 additions & 7 deletions internal/handler/insightsFactsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,26 +41,24 @@ 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
}
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
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 2 additions & 4 deletions internal/handler/insightsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
Expand All @@ -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())
}
Expand Down

0 comments on commit 62b43e1

Please sign in to comment.