-
Notifications
You must be signed in to change notification settings - Fork 285
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
refactor: replace panic with error in rules #1126
refactor: replace panic with error in rules #1126
Conversation
As is, the implementation of rules in this branch does not handle errors but put them "under the carpet" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't fun to review 😅 27 minutes
# Conflicts: # rule/add_constant.go # rule/argument_limit.go # rule/banned_characters.go # rule/cognitive_complexity.go # rule/comment_spacings.go # rule/comments_density.go # rule/context_as_argument.go # rule/cyclomatic.go # rule/defer.go # rule/dot_imports.go # rule/enforce_map_style.go # rule/enforce_repeated_arg_type_style.go # rule/enforce_slice_style.go # rule/error_strings.go # rule/exported.go # rule/file_header.go # rule/file_length_limit.go # rule/filename_format.go # rule/function_length.go # rule/function_result_limit.go # rule/import_alias_naming.go # rule/imports_blocklist.go # rule/line_length_limit.go # rule/max_control_nesting.go # rule/max_public_structs.go # rule/receiver_naming.go # rule/struct_tag.go # rule/unchecked_type_assertion.go # rule/unhandled_error.go # rule/unused_param.go # rule/unused_receiver.go # rule/var_naming.go
lint/rule.go
Outdated
@@ -14,7 +14,7 @@ type DisabledInterval struct { | |||
// Rule defines an abstract rule interface | |||
type Rule interface { | |||
Name() string | |||
Apply(*File, Arguments) []Failure | |||
Apply(*File, Arguments) ([]Failure, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't simply add a new return argument to the Apply
function due to the backward compatibility. Someone who uses revivelib
may depend on this interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok @alexandear any idea how to handle errors without of course excuting panic(), we can use in each Apply:
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
but it is not elegant way :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To maintain backwards compatibility we would have to add backwards compatible wrappers around the newly included changes. And one thing that came to my mind, how do these changes fit the use of Revive
in golangci-lint
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok got it @denisvmedia you want something like that: https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/revive/revive.go#L53 ? but is it possible to do that in separate PR, or it should be done in that PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mfederowicz thanks for the updates. Please run
On the branch, you will see that the problem of error messages in uppercase is present in almost all configuration error messages nit: we could add an utility function (in rules/utils.go ?) to avoid repeating |
ok @chavacava i replaced almost all errors msg in rules, but there are some cases in testdata what about them? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some details to adjust but overall OK.
Thanks @mfederowicz !
You did great, you managed to fix the panic issue. I will try to open a PR with ideas I have.
I cannot only suggest things, I have to code sometimes 🤭 Then I will be the one who will take feedbacks from you guys 😅 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
var configureErr error | ||
r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) | ||
|
||
if configureErr != nil { | ||
return newInternalFailureError(configureErr) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose adding a test to check a situation where configureErr
is not nil
. Not for all rules but at least for a one.
@@ -43,10 +47,9 @@ func (StringFormatRule) ParseArgumentsTest(arguments lint.Arguments) *string { | |||
// Parse the arguments in a goroutine, defer a recover() call, return the error encountered (or nil if there was no error) | |||
go func() { | |||
defer func() { | |||
err := recover() | |||
err := w.parseArguments(arguments) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we changed configure
to return error
instead of panicking, the ParseArgumentsTest
became unnecessary. We can remove it.
@mfederowicz I'll wait the OK from @alexandear to merge the PR. Thanks for the huge amount of work! And, of course, thanks for the reviews and discussions @ccoVeille @alexandear @denisvmedia |
# Conflicts: # rule/string_format.go
Co-authored-by: Oleksandr Redko <[email protected]>
related with PR #1107
changes in areas:
I am open to suggestion what to change in next steps @chavacava @denisvmedia