From 345c74875de2843cec9453e9c9f28e2bd6b31d4d Mon Sep 17 00:00:00 2001 From: nathanmartinszup <63246935+nathanmartinszup@users.noreply.github.com> Date: Fri, 4 Mar 2022 14:29:21 -0300 Subject: [PATCH] analyzer:bugfix - separate warnings from errors (#1013) Both errors and warnings are handled using the same field, this causes a lot of confusion and a very complex code. This pull request removes warnings from the errors field, and adds them to the specific field for warnings. In the future with the refactoring of formaters this code can be removed, but for now it is a necessary workaround. Signed-off-by: Nathan Martins --- internal/controllers/analyzer/analyzer.go | 24 +++++++++++++++++++ .../controllers/printresults/print_results.go | 12 ++-------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/internal/controllers/analyzer/analyzer.go b/internal/controllers/analyzer/analyzer.go index 38e4305b3..af0a5acd3 100644 --- a/internal/controllers/analyzer/analyzer.go +++ b/internal/controllers/analyzer/analyzer.go @@ -250,6 +250,7 @@ func (a *Analyzer) setVulnerabilityType( func (a *Analyzer) setAnalysisFinishedData() *analysis.Analysis { a.analysis.FinishedAt = time.Now() + a.removeWarningsFromErrors() if a.analysis.HasErrors() { a.analysis.Status = enumsAnalysis.Error return a.analysis @@ -430,3 +431,26 @@ func (a *Analyzer) getAllConfigHashes() []string { return configHashes } + +// removeWarningsFromErrors workaround to separate warnings from errors until the formatters are refactored +func (a *Analyzer) removeWarningsFromErrors() { + var errors string + + for _, err := range strings.SplitAfter(a.analysis.Errors, ";") { + if a.isWarning(err) { + a.analysis.AddWarning(err) + } else { + errors += err + } + } + + a.analysis.Errors = errors +} + +// isWarning workaround to check if the message it's form a warning until the formatters are refactored +func (a *Analyzer) isWarning(err string) bool { + return strings.Contains(err, messages.MsgErrorPacketJSONNotFound) || + strings.Contains(err, messages.MsgErrorYarnLockNotFound) || + strings.Contains(err, messages.MsgErrorGemLockNotFound) || + strings.Contains(err, messages.MsgErrorNotFoundRequirementsTxt) +} diff --git a/internal/controllers/printresults/print_results.go b/internal/controllers/printresults/print_results.go index 97d271913..ed0f1426e 100644 --- a/internal/controllers/printresults/print_results.go +++ b/internal/controllers/printresults/print_results.go @@ -376,16 +376,8 @@ func (pr *PrintResults) checkIfExistsErrorsInAnalysis() { } } -func (pr *PrintResults) printErrors(errorMessage string) { - if strings.Contains(errorMessage, messages.MsgErrorPacketJSONNotFound) || - strings.Contains(errorMessage, messages.MsgErrorYarnLockNotFound) || - strings.Contains(errorMessage, messages.MsgErrorGemLockNotFound) || - strings.Contains(errorMessage, messages.MsgErrorNotFoundRequirementsTxt) { - logger.LogWarnWithLevel(strings.ReplaceAll(errorMessage, ";", "")) - return - } - - logger.LogStringAsError(strings.ReplaceAll(errorMessage, ";", "")) +func (pr *PrintResults) printErrors(err string) { + logger.LogStringAsError(strings.ReplaceAll(err, ";", "")) } func (pr *PrintResults) printResponseAnalysis() {