Skip to content

Commit

Permalink
feat: modify error handling to return a list of errors to display to …
Browse files Browse the repository at this point in the history
…the user at the end of analysis without blocking it if an error is detected (e.g., version of an object is not available on user's cluster)

Signed-off-by: Matthis <matthish29@gmail.com>
  • Loading branch information
matthisholleville committed Apr 20, 2023
1 parent df2ed41 commit fa087ff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
9 changes: 5 additions & 4 deletions cmd/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ var AnalyzeCmd = &cobra.Command{
os.Exit(1)
}

err = config.RunAnalysis()
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
analysisErrors := config.RunAnalysis()
if len(analysisErrors) != 0 {
for _, err := range analysisErrors {
color.Red("Error: %s", err)
}
}

if explain {
Expand Down
19 changes: 11 additions & 8 deletions pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"reflect"
"strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -95,7 +96,7 @@ func NewAnalysis(backend string, language string, filters []string, namespace st
}, nil
}

func (a *Analysis) RunAnalysis() error {
func (a *Analysis) RunAnalysis() []error {
activeFilters := viper.GetStringSlice("active_filters")

analyzerMap := analyzer.GetAnalyzerMap()
Expand All @@ -107,16 +108,18 @@ func (a *Analysis) RunAnalysis() error {
AIClient: a.AIClient,
}

var errorList []error

// if there are no filters selected and no active_filters then run all of them
if len(a.Filters) == 0 && len(activeFilters) == 0 {
for _, analyzer := range analyzerMap {
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
return err
errorList = append(errorList, errors.New(fmt.Sprintf("[%s] %s", reflect.TypeOf(analyzer).Name(), err)))
}
a.Results = append(a.Results, results...)
}
return nil
return errorList
}

// if the filters flag is specified
Expand All @@ -125,27 +128,27 @@ func (a *Analysis) RunAnalysis() error {
if analyzer, ok := analyzerMap[filter]; ok {
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
return err
errorList = append(errorList, errors.New(fmt.Sprintf("[%s] %s", filter, err)))
}
a.Results = append(a.Results, results...)
} else {
return errors.New(fmt.Sprintf("\"%s\" filter does not exist. Please run k8sgpt filters list.", filter))
errorList = append(errorList, errors.New(fmt.Sprintf("\"%s\" filter does not exist. Please run k8sgpt filters list.", filter)))
}
}
return nil
return errorList
}

// use active_filters
for _, filter := range activeFilters {
if analyzer, ok := analyzerMap[filter]; ok {
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
return err
errorList = append(errorList, errors.New(fmt.Sprintf("[%s] %s", filter, err)))
}
a.Results = append(a.Results, results...)
}
}
return nil
return errorList
}

func (a *Analysis) GetAIResults(output string, anonymize bool) error {
Expand Down
12 changes: 7 additions & 5 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ func (s *Config) analyzeHandler(w http.ResponseWriter, r *http.Request) {
return
}

err = config.RunAnalysis()
if err != nil {
color.Red("Error: %v", err)
analysisErrors := config.RunAnalysis()
if analysisErrors != nil {
var errorMessage string
for _, err := range analysisErrors {
errorMessage += err.Error() + "\n"
}
http.Error(w, errorMessage, http.StatusInternalServerError)
health.Failure++
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if explain {
Expand Down

0 comments on commit fa087ff

Please sign in to comment.