Skip to content

Commit

Permalink
lint: replace type assertions and type switch on errors (#3376)
Browse files Browse the repository at this point in the history
* errorlint: replace type assertions on errors
* errorlint: replace type switch on errors
* lint
  • Loading branch information
mmetc authored Dec 27, 2024
1 parent a1d26bd commit fc17c0c
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 65 deletions.
10 changes: 1 addition & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ linters-settings:
arguments: [6]
- name: function-length
# lower this after refactoring
arguments: [110, 237]
arguments: [111, 238]
- name: get-return
disabled: true
- name: increment-decrement
Expand Down Expand Up @@ -333,14 +333,6 @@ issues:
- errorlint
text: "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"

- linters:
- errorlint
text: "type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors"

- linters:
- errorlint
text: "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"

- linters:
- nosprintfhostport
text: "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec/win_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func runService(name string) error {
// All the calls to logging before the logger is configured are pretty much useless, but we keep them for clarity
err := eventlog.InstallAsEventCreate("CrowdSec", eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
if errno, ok := err.(syscall.Errno); ok {
if errno, ok := err.(syscall.Errno); ok { //nolint:errorlint
if errno == windows.ERROR_ACCESS_DENIED {
log.Warnf("Access denied when installing event source, running as non-admin ?")
} else {
Expand Down
3 changes: 2 additions & 1 deletion pkg/acquisition/acquisition.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ func GetMetrics(sources []DataSource, aggregated bool) error {

for _, metric := range metrics {
if err := prometheus.Register(metric); err != nil {
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
var alreadyRegisteredErr prometheus.AlreadyRegisteredError
if !errors.As(err, &alreadyRegisteredErr) {
return fmt.Errorf("could not register metrics for datasource %s: %w", sources[i].GetName(), err)
}
// ignore the error
Expand Down
Loading

0 comments on commit fc17c0c

Please sign in to comment.