Skip to content

Commit

Permalink
Use errors.Is()
Browse files Browse the repository at this point in the history
Add errorlint linter to golangci: only enable the wrapped
error check until we fix all the others.
  • Loading branch information
Bryan Venteicher committed Dec 20, 2024
1 parent 4070d54 commit 0af0b3f
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 12 deletions.
7 changes: 6 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,16 @@ linters-settings:
desc: "replaced by internal packages like pkg/util/ptr"
- pkg: github.com/onsi/ginkgo$
desc: "replaced by github.com/onsi/ginkgo/v2"

errorlint:
# Check whether fmt.Errorf uses the %w verb for formatting errors.
errorf: false
# Check for plain type assertions and type switches.
asserts: false

linters:
disable-all: true
enable:
- errorlint
- asciicheck
- bodyclose
- depguard
Expand Down
3 changes: 2 additions & 1 deletion cmd/web-console-validator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"errors"
"flag"
"net/http"
"os"
Expand Down Expand Up @@ -93,7 +94,7 @@ func main() {
}

logger.Info("Starting the web-console validation server", "port", *serverPort, "path", *serverPath)
if err := server.Run(); err != nil && err != http.ErrServerClosed {
if err := server.Run(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error(err, "Failed to run the web-console validation server")
os.Exit(1)
}
Expand Down
5 changes: 3 additions & 2 deletions controllers/infra/zone/zone_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package zone

import (
"context"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -131,7 +132,7 @@ func (r *Reconciler) ReconcileDelete(
},
fmt.Sprintf("%s/%s", obj.Namespace, obj.Name)); err != nil {

if err != watcher.ErrAsyncSignalDisabled {
if !errors.Is(err, watcher.ErrAsyncSignalDisabled) {
return ctrl.Result{}, err
}
}
Expand All @@ -157,7 +158,7 @@ func (r *Reconciler) ReconcileNormal(
},
fmt.Sprintf("%s/%s", obj.Namespace, obj.Name)); err != nil {

if err != watcher.ErrAsyncSignalDisabled {
if !errors.Is(err, watcher.ErrAsyncSignalDisabled) {
return ctrl.Result{}, err
}
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/util/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package encoding
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -33,7 +34,7 @@ func DecodeYAML(data []byte) (<-chan *unstructured.Unstructured, <-chan error) {
for {
buf, err := multidocReader.Read()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return
}
chanErr <- fmt.Errorf("failed to read yaml data: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,10 @@ func getIsDefaultVMClassController(ctx context.Context) bool {
// ignoredCreateErr is written this way in order to illustrate coverage more
// accurately.
func ignoredCreateErr(err error) bool {
if err == providers.ErrDuplicateCreate {
if errors.Is(err, providers.ErrDuplicateCreate) {
return true
}
if err == providers.ErrTooManyCreates {
if errors.Is(err, providers.ErrTooManyCreates) {
return true
}
return false
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/vsphere/vm/power_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func doAndWaitOnSoftPowerOp(
defer cancel()

if err := waitForPowerStateFn(ctx, desiredPowerState); err != nil {
if ctx.Err() == context.DeadlineExceeded {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return 0, fmt.Errorf("timed out waiting for power state %s", desiredPowerState)
}
return 0, fmt.Errorf("failed to wait for power state %s %w", desiredPowerState, err)
Expand Down Expand Up @@ -606,7 +606,7 @@ func doAndWaitOnSoftRestart(
defer cancel()

if err := powerOpFn(ctx); err != nil {
if ctx.Err() == context.DeadlineExceeded {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return 0, errors.New("timed out while soft restarting vm")
}
return 0, fmt.Errorf("failed to soft restart vm %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/vmconfig/crypto/crypto_reconciler_pre.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (r reconciler) reconcileUpdateDefaultKeyProvider(
}

func setConditionAndReturnErr(args reconcileArgs, err error, r Reason) error {
if err == ErrInvalidKeyProvider || err == ErrInvalidKeyID {
if errors.Is(err, ErrInvalidKeyProvider) || errors.Is(err, ErrInvalidKeyID) {
r = ReasonEncryptionClassInvalid
} else if apierrors.IsNotFound(err) {
r = ReasonEncryptionClassNotFound
Expand Down
3 changes: 2 additions & 1 deletion test/builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -111,7 +112,7 @@ func readDocuments(fp string) ([][]byte, error) {
// Read document
doc, err := reader.Read()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion webhooks/virtualmachine/mutation/virtualmachine_mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mutation
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -596,7 +597,7 @@ func SetDefaultEncryptionClass(
k8sClient,
vm.Namespace)
if err != nil {
if err == kubeutil.ErrNoDefaultEncryptionClass {
if errors.Is(err, kubeutil.ErrNoDefaultEncryptionClass) {
return false, nil
}
return false, err
Expand Down

0 comments on commit 0af0b3f

Please sign in to comment.