mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-06-29 08:48:31 +00:00
feat: modify error handling to return a list of errors to display to 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>
This commit is contained in:
parent
df2ed4185b
commit
fa087ff559
@ -36,10 +36,11 @@ var AnalyzeCmd = &cobra.Command{
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = config.RunAnalysis()
|
analysisErrors := config.RunAnalysis()
|
||||||
if err != nil {
|
if len(analysisErrors) != 0 {
|
||||||
color.Red("Error: %v", err)
|
for _, err := range analysisErrors {
|
||||||
os.Exit(1)
|
color.Red("Error: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if explain {
|
if explain {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
@ -95,7 +96,7 @@ func NewAnalysis(backend string, language string, filters []string, namespace st
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Analysis) RunAnalysis() error {
|
func (a *Analysis) RunAnalysis() []error {
|
||||||
activeFilters := viper.GetStringSlice("active_filters")
|
activeFilters := viper.GetStringSlice("active_filters")
|
||||||
|
|
||||||
analyzerMap := analyzer.GetAnalyzerMap()
|
analyzerMap := analyzer.GetAnalyzerMap()
|
||||||
@ -107,16 +108,18 @@ func (a *Analysis) RunAnalysis() error {
|
|||||||
AIClient: a.AIClient,
|
AIClient: a.AIClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errorList []error
|
||||||
|
|
||||||
// if there are no filters selected and no active_filters then run all of them
|
// if there are no filters selected and no active_filters then run all of them
|
||||||
if len(a.Filters) == 0 && len(activeFilters) == 0 {
|
if len(a.Filters) == 0 && len(activeFilters) == 0 {
|
||||||
for _, analyzer := range analyzerMap {
|
for _, analyzer := range analyzerMap {
|
||||||
results, err := analyzer.Analyze(analyzerConfig)
|
results, err := analyzer.Analyze(analyzerConfig)
|
||||||
if err != nil {
|
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...)
|
a.Results = append(a.Results, results...)
|
||||||
}
|
}
|
||||||
return nil
|
return errorList
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the filters flag is specified
|
// if the filters flag is specified
|
||||||
@ -125,14 +128,14 @@ func (a *Analysis) RunAnalysis() error {
|
|||||||
if analyzer, ok := analyzerMap[filter]; ok {
|
if analyzer, ok := analyzerMap[filter]; ok {
|
||||||
results, err := analyzer.Analyze(analyzerConfig)
|
results, err := analyzer.Analyze(analyzerConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
errorList = append(errorList, errors.New(fmt.Sprintf("[%s] %s", filter, err)))
|
||||||
}
|
}
|
||||||
a.Results = append(a.Results, results...)
|
a.Results = append(a.Results, results...)
|
||||||
} else {
|
} 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
|
// use active_filters
|
||||||
@ -140,12 +143,12 @@ func (a *Analysis) RunAnalysis() error {
|
|||||||
if analyzer, ok := analyzerMap[filter]; ok {
|
if analyzer, ok := analyzerMap[filter]; ok {
|
||||||
results, err := analyzer.Analyze(analyzerConfig)
|
results, err := analyzer.Analyze(analyzerConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
errorList = append(errorList, errors.New(fmt.Sprintf("[%s] %s", filter, err)))
|
||||||
}
|
}
|
||||||
a.Results = append(a.Results, results...)
|
a.Results = append(a.Results, results...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return errorList
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Analysis) GetAIResults(output string, anonymize bool) error {
|
func (a *Analysis) GetAIResults(output string, anonymize bool) error {
|
||||||
|
@ -55,12 +55,14 @@ func (s *Config) analyzeHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = config.RunAnalysis()
|
analysisErrors := config.RunAnalysis()
|
||||||
if err != nil {
|
if analysisErrors != nil {
|
||||||
color.Red("Error: %v", err)
|
var errorMessage string
|
||||||
|
for _, err := range analysisErrors {
|
||||||
|
errorMessage += err.Error() + "\n"
|
||||||
|
}
|
||||||
|
http.Error(w, errorMessage, http.StatusInternalServerError)
|
||||||
health.Failure++
|
health.Failure++
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if explain {
|
if explain {
|
||||||
|
Loading…
Reference in New Issue
Block a user