Skip to content

Commit

Permalink
analyzer:bugfix - separate warnings from errors (#1013)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
nathanmartinszup authored Mar 4, 2022
1 parent df2e20c commit 345c748
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
24 changes: 24 additions & 0 deletions internal/controllers/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
12 changes: 2 additions & 10 deletions internal/controllers/printresults/print_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 345c748

Please # to comment.