Skip to content

Commit 345c748

Browse files
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 <nathan.martins@zup.com.br>
1 parent df2e20c commit 345c748

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

internal/controllers/analyzer/analyzer.go

+24
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ func (a *Analyzer) setVulnerabilityType(
250250
func (a *Analyzer) setAnalysisFinishedData() *analysis.Analysis {
251251
a.analysis.FinishedAt = time.Now()
252252

253+
a.removeWarningsFromErrors()
253254
if a.analysis.HasErrors() {
254255
a.analysis.Status = enumsAnalysis.Error
255256
return a.analysis
@@ -430,3 +431,26 @@ func (a *Analyzer) getAllConfigHashes() []string {
430431

431432
return configHashes
432433
}
434+
435+
// removeWarningsFromErrors workaround to separate warnings from errors until the formatters are refactored
436+
func (a *Analyzer) removeWarningsFromErrors() {
437+
var errors string
438+
439+
for _, err := range strings.SplitAfter(a.analysis.Errors, ";") {
440+
if a.isWarning(err) {
441+
a.analysis.AddWarning(err)
442+
} else {
443+
errors += err
444+
}
445+
}
446+
447+
a.analysis.Errors = errors
448+
}
449+
450+
// isWarning workaround to check if the message it's form a warning until the formatters are refactored
451+
func (a *Analyzer) isWarning(err string) bool {
452+
return strings.Contains(err, messages.MsgErrorPacketJSONNotFound) ||
453+
strings.Contains(err, messages.MsgErrorYarnLockNotFound) ||
454+
strings.Contains(err, messages.MsgErrorGemLockNotFound) ||
455+
strings.Contains(err, messages.MsgErrorNotFoundRequirementsTxt)
456+
}

internal/controllers/printresults/print_results.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,8 @@ func (pr *PrintResults) checkIfExistsErrorsInAnalysis() {
376376
}
377377
}
378378

379-
func (pr *PrintResults) printErrors(errorMessage string) {
380-
if strings.Contains(errorMessage, messages.MsgErrorPacketJSONNotFound) ||
381-
strings.Contains(errorMessage, messages.MsgErrorYarnLockNotFound) ||
382-
strings.Contains(errorMessage, messages.MsgErrorGemLockNotFound) ||
383-
strings.Contains(errorMessage, messages.MsgErrorNotFoundRequirementsTxt) {
384-
logger.LogWarnWithLevel(strings.ReplaceAll(errorMessage, ";", ""))
385-
return
386-
}
387-
388-
logger.LogStringAsError(strings.ReplaceAll(errorMessage, ";", ""))
379+
func (pr *PrintResults) printErrors(err string) {
380+
logger.LogStringAsError(strings.ReplaceAll(err, ";", ""))
389381
}
390382

391383
func (pr *PrintResults) printResponseAnalysis() {

0 commit comments

Comments
 (0)