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

chore: use errors.New to replace fmt.Errorf with no parameters will much better #5377

Closed
wants to merge 3 commits into from

Conversation

ChengenH
Copy link

use errors.New to replace fmt.Errorf with no parameters will much better

@ChengenH ChengenH requested a review from a team as a code owner April 21, 2024 02:02
@ChengenH ChengenH changed the title chore: use errors.New to replace fmt.Errorf with no parameters will m… chore: use errors.New to replace fmt.Errorf with no parameters will much better Apr 21, 2024
Copy link
Contributor

@mmorel-35 mmorel-35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t there any linter like errorlint or gosimple that could ensure such best practice ?

@ChengenH
Copy link
Author

Isn’t there any linter like errorlint that could ensure such best practice ?

It's not serious, so...

@mmorel-35
Copy link
Contributor

"Heaven is in the details" would say Uncle Bob 😉

Copy link
Member

@yurishkuro yurishkuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're interested in this clean-up, there are many other places with similar usage:

./model/ids.go:90:		return TraceID{}, fmt.Errorf("invalid length for TraceID")
./model/ids.go:97:	return nil, fmt.Errorf("unsupported method TraceID.MarshalText; please use github.com/gogo/protobuf/jsonpb for marshalling")
./model/ids.go:102:	return fmt.Errorf("unsupported method TraceID.UnmarshalText; please use github.com/gogo/protobuf/jsonpb for marshalling")
./model/ids.go:127:		return 0, fmt.Errorf("buffer is too short")
./model/ids.go:184:		return SpanID(0), fmt.Errorf("invalid length for SpanID")
./model/ids.go:191:	return nil, fmt.Errorf("unsupported method SpanID.MarshalText; please use github.com/gogo/protobuf/jsonpb for marshalling")
./model/ids.go:196:	return fmt.Errorf("unsupported method SpanID.UnmarshalText; please use github.com/gogo/protobuf/jsonpb for marshalling")
./pkg/config/tlscfg/options.go:78:			return nil, fmt.Errorf("minimum tls version can't be greater than maximum tls version")
./pkg/config/tlscfg/options.go:108:		return nil, fmt.Errorf("for client auth via TLS, either both client certificate and key must be supplied, or neither")
./pkg/config/tlscfg/options_test.go:162:					return nil, fmt.Errorf("fake system pool")
./pkg/clientcfg/clientcfghttp/thrift-0.9.2/ttypes.go:59:	return SamplingStrategyType(0), fmt.Errorf("not a valid SamplingStrategyType string")
./pkg/es/errors_test.go:15:	require.ErrorContains(t, fmt.Errorf("some err"), "some err", "no panic")
./pkg/es/config/config.go:366:		return nil, fmt.Errorf("both Password and PasswordFilePath are set")

Copy link

codecov bot commented Jun 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.13%. Comparing base (44484ac) to head (3bfd244).

❗ There is a different number of reports uploaded between BASE (44484ac) and HEAD (3bfd244). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (44484ac) HEAD (3bfd244)
unittests 1 0
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5377       +/-   ##
===========================================
- Coverage   96.38%   54.13%   -42.25%     
===========================================
  Files         329      163      -166     
  Lines       16060     8412     -7648     
===========================================
- Hits        15479     4554    -10925     
- Misses        404     3414     +3010     
- Partials      177      444      +267     
Flag Coverage Δ
badger_v1 8.04% <ø> (ø)
badger_v2 1.92% <ø> (ø)
cassandra-3.x-v1 16.60% <ø> (ø)
cassandra-3.x-v2 1.84% <ø> (ø)
cassandra-4.x-v1 16.60% <ø> (ø)
cassandra-4.x-v2 1.84% <ø> (ø)
elasticsearch-7.x-v1 18.87% <ø> (ø)
elasticsearch-8.x-v1 19.07% <ø> (-0.02%) ⬇️
elasticsearch-8.x-v2 19.07% <ø> (-0.02%) ⬇️
grpc_v1 9.46% <ø> (-0.02%) ⬇️
grpc_v2 7.47% <ø> (-0.02%) ⬇️
kafka 9.76% <ø> (ø)
opensearch-1.x-v1 18.93% <ø> (+0.01%) ⬆️
opensearch-2.x-v1 18.92% <ø> (-0.02%) ⬇️
opensearch-2.x-v2 18.93% <ø> (+0.01%) ⬆️
unittests ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yurishkuro yurishkuro mentioned this pull request Jun 2, 2024
@yurishkuro
Copy link
Member

alternatively, I agree with @mmorel-35 , we should find a linter that catches this, rather than doing manually. I thought revive linter would do that (#5505), but it doesn't.

yurishkuro added a commit that referenced this pull request Jun 2, 2024
## Which problem is this PR solving?
- revive is a pretty comprehensive linter that catches many issues
- incidentally, I wanted to have a linter for the change in #5377, but
the `errorf` [rule in
revive](https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#errorf)
is only catching `errors.New(fmt.Sprintf())`, not
`fmt.Errorf(just_string)` scenarios.

## Description of the changes
- add revive to the main list of linters
- disable some of its rules which are currently breaking for future
clean-up
- clean-up a few places what had low number of breaks

## How was this change tested?
- `make lint`

Signed-off-by: Yuri Shkuro <[email protected]>
@yurishkuro
Copy link
Member

no follow-up, closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants