Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

None of the error returned by reporters are checked #134

Closed
ccoVeille opened this issue Mar 30, 2024 · 7 comments
Closed

None of the error returned by reporters are checked #134

ccoVeille opened this issue Mar 30, 2024 · 7 comments
Labels
has-pr This issue has an associated PR validator-core Issues that relate to the core validator functionality

Comments

@ccoVeille
Copy link
Contributor

ccoVeille commented Mar 30, 2024

In pkg/cli/cli.go there is a section where none of the reporter.Print* method that could return errors are checked

if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintSingleGroupJson(reportGroup)
} else if !Quiet {
reporter.PrintSingleGroupStdout(reportGroup)
}
} else if len(GroupOutput) == 2 {
reportGroup, err := GroupByDouble(reports, GroupOutput)
if err != nil {
return 1, fmt.Errorf("unable to group by double value: %v", err)
}
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintDoubleGroupJson(reportGroup)
} else if !Quiet {
reporter.PrintDoubleGroupStdout(reportGroup)
}
} else if len(GroupOutput) == 3 {
reportGroup, err := GroupByTriple(reports, GroupOutput)
if err != nil {
return 1, fmt.Errorf("unable to group by triple value: %v", err)
}
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintTripleGroupJson(reportGroup)
} else if !Quiet {
reporter.PrintTripleGroupStdout(reportGroup)
}

This code requires a bit of refactoring, I think.

The addition of quiet mode (great addition BTW) with #124 made it even more complex to read and maintain.

@ccoVeille ccoVeille mentioned this issue Mar 30, 2024
@HakanVardarr
Copy link
Contributor

I added a new pull request. I did some refactoring on cli.go can you look it?

@ccoVeille
Copy link
Contributor Author

I added a new pull request. I did some refactoring on cli.go can you look it?

Can you reference the link? Because I looked for it and didn't see it.

@HakanVardarr
Copy link
Contributor

I added a new pull request. I did some refactoring on cli.go can you look it?

Can you reference the link? Because I looked for it and didn't see it.

#131 (comment)

@ccoVeille
Copy link
Contributor Author

I like the refactoring you initiated, but it doesn't address the issue I raised here

Please look at this

if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintSingleGroupJson(reportGroup.(map[string][]reporter.Report))
} else {
reporter.PrintSingleGroupStdout(reportGroup.(map[string][]reporter.Report))
}

Either reporter.PrintSingleGroupJson and reporter.PrintSingleGroupStdout may return an error.

and the error are not tested.

Your code is now simpler, so it's good, but you should consider something like this

       if len(GroupOutput) == 1 && GroupOutput[0] != "" {
		// Check reporter type to determine how to print
		if _, ok := c.Reporter.(reporter.JsonReporter); ok {
			err = reporter.PrintSingleGroupJson(reportGroup.(map[string][]reporter.Report))
		} else {
			err = reporter.PrintSingleGroupStdout(reportGroup.(map[string][]reporter.Report))
		}
	} else if len(GroupOutput) == 2 {
		if _, ok := c.Reporter.(reporter.JsonReporter); ok {
			err = reporter.PrintDoubleGroupJson(reportGroup.(map[string]map[string][]reporter.Report))
		} else {
			err = reporter.PrintDoubleGroupStdout(reportGroup.(map[string]map[string][]reporter.Report))
		}
	} else if len(GroupOutput) == 3 {
		if _, ok := c.Reporter.(reporter.JsonReporter); ok {
			err = reporter.PrintTripleGroupJson(reportGroup.(map[string]map[string]map[string][]reporter.Report))
		} else {
			err = reporter.PrintTripleGroupStdout(reportGroup.(map[string]map[string]map[string][]reporter.Report))
		}
	} else {
		err =  c.Reporter.Print(reports)
	}

	if err != nil {
		return err           
        }

	return nil

@HakanVardarr
Copy link
Contributor

Yeah I will return the error. Thanks for the suggestion.

@HakanVardarr
Copy link
Contributor

Instead of

err = reporter.PrintSingleGroupJson(reportGroup.(map[string][]reporter.Report))

Can't we use

return reporter.PrintSingleGroupJson(reportGroup.(map[string][]reporter.Report))

@ccoVeille
Copy link
Contributor Author

You refactored it the right way.

I mean the current version is already applying the pattern you talked about.

So you solved the issue with #131 now, which is great.

I will comment the other PR then, because I have a code suggestion

@kehoecj kehoecj added has-pr This issue has an associated PR validator-core Issues that relate to the core validator functionality labels Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-pr This issue has an associated PR validator-core Issues that relate to the core validator functionality
Projects
None yet
Development

No branches or pull requests

3 participants