Skip to content

Commit

Permalink
Replace many minor usages of errors with errkit
Browse files Browse the repository at this point in the history
  • Loading branch information
e-sumin committed Nov 22, 2024
1 parent fee393d commit 23241c5
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 45 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/jpillora/backoff v1.0.0
github.com/json-iterator/go v1.1.12
github.com/kanisterio/errkit v0.0.2
github.com/kanisterio/safecli v0.0.8
github.com/kanisterio/safecli v0.0.9-0.20241121134252-2ffd9801dcad
github.com/kopia/kopia v0.17.1-0.20240927044625-1bceb7155ede
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0
github.com/lib/pq v1.10.9
Expand All @@ -42,7 +42,7 @@ require (
//pinned openshift to release-4.5 branch
github.com/openshift/api v0.0.0-20231222123017-053aee22b4b4
github.com/openshift/client-go v0.0.0-20231221125933-2aa81c72f992
github.com/pkg/errors v0.9.1
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.5
github.com/prometheus/client_model v0.6.1
github.com/sirupsen/logrus v1.9.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kanisterio/errkit v0.0.2 h1:3v3HGz9lHIbZR6Jr9qIJpRjaqUX0rsJSLMEQGsMHiUk=
github.com/kanisterio/errkit v0.0.2/go.mod h1:ViQ6kPJ2gTJDEvRytmwde7pzG9/sndObF9BPZoEZixc=
github.com/kanisterio/safecli v0.0.8 h1:flvTiGksy/a0+zvqjaBSJwxESu/nFcG65yttmR0XwiA=
github.com/kanisterio/safecli v0.0.8/go.mod h1:KBraqj8mdv2cwAr9wecknGUb8jztTzUik0r7uE6yRA8=
github.com/kanisterio/safecli v0.0.9-0.20241121134252-2ffd9801dcad h1:JaC21jpch32i3hfUE3m1So06CvPoi+sdQ2KKIRCRKCo=
github.com/kanisterio/safecli v0.0.9-0.20241121134252-2ffd9801dcad/go.mod h1:y1oYVoT2eqsmySCIS5yOzrxaYVywlMSEdWEKfLhBjgs=
github.com/kastenhq/check v0.0.0-20180626002341-0264cfcea734 h1:qulsCaCv+O2y9/sQ9nd5KChnAgFOWakTHQ9ZADjs6DQ=
github.com/kastenhq/check v0.0.0-20180626002341-0264cfcea734/go.mod h1:rdqSnvOJuKCPFW/h2rVLuXOAkRnHHdp9PZcKx4HCoDM=
github.com/kastenhq/stow v0.2.6-kasten.1.0.20231101232131-9321daa23aae h1:2cl4yuAJpdmLCx7G8eIsfNlQBLEfw0JDj6mTTyqc5qg=
Expand Down
9 changes: 4 additions & 5 deletions pkg/kanx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kanx
import (
"bytes"
"context"
"fmt"
"io"
"net"
"os"
Expand All @@ -12,7 +11,7 @@ import (
"syscall"
"time"

"github.com/pkg/errors"
"github.com/kanisterio/errkit"
"google.golang.org/grpc"

"github.com/kanisterio/kanister/pkg/field"
Expand Down Expand Up @@ -117,12 +116,12 @@ func (s *processServiceServer) ListProcesses(lpr *ListProcessesRequest, lps Proc
return nil
}

var errProcessNotFound = fmt.Errorf("Process not found")
var errProcessNotFound = errkit.NewSentinelErr("Process not found")

func (s *processServiceServer) Stdout(por *ProcessOutputRequest, ss ProcessService_StdoutServer) error {
p, ok := s.processes[por.Pid]
if !ok {
return errors.WithStack(errProcessNotFound)
return errkit.WithStack(errProcessNotFound)
}
fh, err := os.Open(p.stdout.Name())
if err != nil {
Expand All @@ -134,7 +133,7 @@ func (s *processServiceServer) Stdout(por *ProcessOutputRequest, ss ProcessServi
func (s *processServiceServer) Stderr(por *ProcessOutputRequest, ss ProcessService_StderrServer) error {
p, ok := s.processes[por.Pid]
if !ok {
return errors.WithStack(errProcessNotFound)
return errkit.WithStack(errProcessNotFound)
}
fh, err := os.Open(p.stderr.Name())
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions pkg/kopia/cli/internal/test/command_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package test

import (
"github.com/kanisterio/errkit"
"github.com/kanisterio/safecli"
"github.com/kanisterio/safecli/test"
"github.com/pkg/errors"
"gopkg.in/check.v1"
)

Expand Down Expand Up @@ -62,8 +62,7 @@ func (t *CommandTest) assertNoError(c *check.C, err error) {

// assertError checks the error against ExpectedErr.
func (t *CommandTest) assertError(c *check.C, err error) {
actualErr := errors.Cause(err)
c.Assert(actualErr, check.Equals, t.ExpectedErr)
c.Assert(errkit.Is(err, t.ExpectedErr), check.Equals, true)
}

// assertCLI asserts the builder's CLI output against ExpectedCLI.
Expand Down
4 changes: 2 additions & 2 deletions pkg/ksprig/fipsonly_sprig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package ksprig_test

import (
"errors"
"strings"
"testing"
"text/template"

"github.com/kanisterio/errkit"
"gopkg.in/check.v1"

"github.com/kanisterio/kanister/pkg/ksprig"
Expand Down Expand Up @@ -74,7 +74,7 @@ func (f *FipsOnlySprigSuite) TestUnsupportedTxtFuncMapUsage(c *check.C) {
err = temp.Execute(nil, "")

var sprigErr ksprig.UnsupportedSprigUsageErr
c.Assert(errors.As(err, &sprigErr), check.Equals, true)
c.Assert(errkit.As(err, &sprigErr), check.Equals, true)
c.Assert(sprigErr.Usage, check.Equals, tc.usageErr)
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kube/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ package kube
import (
"bytes"
"context"
"errors"
"strings"
"time"

"github.com/kanisterio/errkit"
"gopkg.in/check.v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -191,7 +191,7 @@ func (s *ExecSuite) TestErrorInExecWithOptions(c *check.C) {
c.Assert(err1, check.Not(check.IsNil))

var ee1 *ExecError
ok := errors.As(err1, &ee1)
ok := errkit.As(err1, &ee1)
c.Assert(ok, check.Equals, true)
c.Assert(ee1.Stdout(), check.Not(check.Equals), testCase.expectedOut)
c.Assert(ee1.Stderr(), check.Not(check.Equals), testCase.expectedErr)
Expand All @@ -208,7 +208,7 @@ func (s *ExecSuite) TestErrorInExecWithOptions(c *check.C) {
c.Assert(err2, check.Not(check.IsNil))

var ee2 *ExecError
ok = errors.As(err2, &ee2)
ok = errkit.As(err2, &ee2)
c.Assert(ok, check.Equals, true)

// When error happens, stdout/stderr buffers should contain all lines produced by an app
Expand Down
4 changes: 2 additions & 2 deletions pkg/kube/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func checkPVCAndPVStatus(ctx context.Context, vol corev1.Volume, p *corev1.Pod,
pvcName := vol.VolumeSource.PersistentVolumeClaim.ClaimName
pvc, err := cli.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, pvcName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(errors.Cause(err)) {
if apierrors.IsNotFound(err) {
// Do not return err, wait for timeout, since sometimes in case of statefulsets, they trigger creation of a volume
return nil
}
Expand All @@ -469,7 +469,7 @@ func checkPVCAndPVStatus(ctx context.Context, vol corev1.Volume, p *corev1.Pod,
}
pv, err := cli.CoreV1().PersistentVolumes().Get(ctx, pvName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(errors.Cause(err)) {
if apierrors.IsNotFound(err) {
// wait for timeout
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kube/pod_command_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package kube
import (
"bytes"
"context"
"errors"
"os"
"sync"
"time"

"github.com/kanisterio/errkit"
"gopkg.in/check.v1"
"k8s.io/client-go/kubernetes/fake"
)
Expand Down Expand Up @@ -129,7 +129,7 @@ func (s *PodCommandExecutorTestSuite) TestPodRunnerExec(c *check.C) {
prp.execWithOptionsSyncEnd.Sync()

c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, context.DeadlineExceeded), check.Equals, true)
c.Assert(errkit.Is(err, context.DeadlineExceeded), check.Equals, true)
},
"Cancelled": func(ctx context.Context, pr PodCommandExecutor, prp *fakePodCommandExecutorProcessor) {
var err error
Expand All @@ -151,7 +151,7 @@ func (s *PodCommandExecutorTestSuite) TestPodRunnerExec(c *check.C) {
prp.execWithOptionsSyncEnd.Sync() // Release ExecWithOptions

c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, context.Canceled), check.Equals, true)
c.Assert(errkit.Is(err, context.Canceled), check.Equals, true)
},
"Successful execution": func(ctx context.Context, pr PodCommandExecutor, prp *fakePodCommandExecutorProcessor) {
var err error
Expand Down
21 changes: 10 additions & 11 deletions pkg/kube/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package kube

import (
"context"
"errors"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -53,7 +52,7 @@ func (s *PodControllerTestSuite) TestPodControllerStartPod(c *check.C) {
pcp.CreatePodErr = simulatedError
err := pc.StartPod(ctx)
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, simulatedError), check.Equals, true)
c.Assert(errkit.Is(err, simulatedError), check.Equals, true)
c.Assert(pcp.InCreatePodOptions, check.DeepEquals, &PodOptions{
Namespace: podControllerNS,
Name: podControllerPodName,
Expand Down Expand Up @@ -85,7 +84,7 @@ func (s *PodControllerTestSuite) TestPodControllerStartPod(c *check.C) {

err = pr.StartPod(ctx)
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, ErrPodControllerPodAlreadyStarted), check.Equals, true)
c.Assert(errkit.Is(err, ErrPodControllerPodAlreadyStarted), check.Equals, true)
c.Assert(prp.InCreatePodOptions, check.IsNil)
},
}
Expand Down Expand Up @@ -114,7 +113,7 @@ func (s *PodControllerTestSuite) TestPodControllerWaitPod(c *check.C) {
"Waiting failed because pod not started yet": func(pcp *FakePodControllerProcessor, pc PodController) {
err := pc.WaitForPodReady(ctx)
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, ErrPodControllerPodNotStarted), check.Equals, true)
c.Assert(errkit.Is(err, ErrPodControllerPodNotStarted), check.Equals, true)
c.Assert(pcp.InCreatePodOptions, check.IsNil)
},
"Waiting failed due to timeout": func(pcp *FakePodControllerProcessor, pc PodController) {
Expand All @@ -132,7 +131,7 @@ func (s *PodControllerTestSuite) TestPodControllerWaitPod(c *check.C) {
c.Assert(err, check.Not(check.IsNil))
c.Assert(pcp.InWaitForPodReadyPodName, check.Equals, podControllerPodName)
c.Assert(pcp.InWaitForPodReadyNamespace, check.Equals, podControllerNS)
c.Assert(errors.Is(err, pcp.WaitForPodReadyErr), check.Equals, true)
c.Assert(errkit.Is(err, pcp.WaitForPodReadyErr), check.Equals, true)

c.Assert(err.Error(), check.Equals, fmt.Sprintf("Pod failed to become ready in time: %s", simulatedError.Error()))
// Check that POD deletion was also invoked with expected arguments
Expand Down Expand Up @@ -175,7 +174,7 @@ func (s *PodControllerTestSuite) TestPodControllerStopPod(c *check.C) {
"Pod not started yet": func(pcp *FakePodControllerProcessor, pc PodController) {
err := pc.StopPod(ctx, 30*time.Second, int64(0))
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, ErrPodControllerPodNotStarted), check.Equals, true)
c.Assert(errkit.Is(err, ErrPodControllerPodNotStarted), check.Equals, true)
c.Assert(pcp.InDeletePodPodName, check.Equals, untouchedStr)
c.Assert(pcp.InDeletePodNamespace, check.Equals, untouchedStr)
},
Expand All @@ -192,7 +191,7 @@ func (s *PodControllerTestSuite) TestPodControllerStopPod(c *check.C) {
pcp.DeletePodErr = simulatedError
err = pc.StopPod(ctx, 30*time.Second, int64(0))
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, simulatedError), check.Equals, true)
c.Assert(errkit.Is(err, simulatedError), check.Equals, true)
},
"Pod successfully deleted": func(pcp *FakePodControllerProcessor, pc PodController) {
pcp.CreatePodRet = &corev1.Pod{
Expand Down Expand Up @@ -239,12 +238,12 @@ func (s *PodControllerTestSuite) TestPodControllerGetCommandExecutorAndFileWrite
pce, err := pc.GetCommandExecutor()
c.Assert(pce, check.IsNil)
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, ErrPodControllerPodNotStarted), check.Equals, true)
c.Assert(errkit.Is(err, ErrPodControllerPodNotStarted), check.Equals, true)

pfw, err := pc.GetFileWriter()
c.Assert(pfw, check.IsNil)
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, ErrPodControllerPodNotStarted), check.Equals, true)
c.Assert(errkit.Is(err, ErrPodControllerPodNotStarted), check.Equals, true)
},
"Pod not ready yet": func(pcp *FakePodControllerProcessor, pc PodController) {
pcp.CreatePodRet = &corev1.Pod{
Expand All @@ -258,12 +257,12 @@ func (s *PodControllerTestSuite) TestPodControllerGetCommandExecutorAndFileWrite
pce, err := pc.GetCommandExecutor()
c.Assert(pce, check.IsNil)
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, ErrPodControllerPodNotReady), check.Equals, true)
c.Assert(errkit.Is(err, ErrPodControllerPodNotReady), check.Equals, true)

pfw, err := pc.GetFileWriter()
c.Assert(pfw, check.IsNil)
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, ErrPodControllerPodNotReady), check.Equals, true)
c.Assert(errkit.Is(err, ErrPodControllerPodNotReady), check.Equals, true)
},
"CommandExecutor successfully returned": func(pcp *FakePodControllerProcessor, pc PodController) {
pcp.CreatePodRet = &corev1.Pod{
Expand Down
5 changes: 2 additions & 3 deletions pkg/kube/pod_file_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package kube
import (
"bytes"
"context"
"errors"
"io"
"os"

Expand Down Expand Up @@ -90,7 +89,7 @@ func (s *PodFileWriterTestSuite) TestPodRunnerWriteFile(c *check.C) {
buf := bytes.NewBuffer([]byte("some file content"))
remover, err := pfw.Write(ctx, "/path/to/file", buf)
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, simulatedError), check.Equals, true)
c.Assert(errkit.Is(err, simulatedError), check.Equals, true)
c.Assert(remover, check.IsNil)

c.Assert(pfwp.podWriter.inWriteNamespace, check.Equals, podFileWriterNS)
Expand Down Expand Up @@ -130,7 +129,7 @@ func (s *PodFileWriterTestSuite) TestPodRunnerWriteFile(c *check.C) {

err = remover.Remove(ctx)
c.Assert(err, check.Not(check.IsNil))
c.Assert(errors.Is(err, simulatedError), check.Equals, true)
c.Assert(errkit.Is(err, simulatedError), check.Equals, true)
c.Assert(pfwp.podWriter.inRemoveNamespace, check.Equals, podFileWriterNS)
c.Assert(pfwp.podWriter.inRemovePodName, check.Equals, podFileWriterPodName)
c.Assert(pfwp.podWriter.inRemoveContainerName, check.Equals, podFileWriterContainerName)
Expand Down
18 changes: 17 additions & 1 deletion pkg/kube/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package kube

import (
"context"
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/kanisterio/errkit"
"gopkg.in/check.v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -1244,3 +1245,18 @@ func (s *PodSuite) TestAddAnnotations(c *check.C) {
c.Assert(tc.podOptions, check.DeepEquals, tc.expectedPodOptions)
}
}

func (s *PodSuite) TestSomething(c *check.C) {
// Create the fake client
fakeClient := fake.NewSimpleClientset()

// Add a reactor to simulate an error when trying to get a PVC
fakeClient.PrependReactor("get", "persistentvolumeclaims", func(action testing.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, apierrors.NewNotFound(action.GetResource().GroupResource(), action.GetSubresource())
})

_, err := fakeClient.CoreV1().PersistentVolumeClaims("abc").Get(context.TODO(), "def", metav1.GetOptions{})
if err != nil {
c.Assert(apierrors.IsNotFound(err), check.Equals, true)
}
}
2 changes: 1 addition & 1 deletion pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"sync"
"testing"
"time"

"github.com/kanisterio/errkit"
"github.com/sirupsen/logrus"
"gopkg.in/check.v1"

Expand Down
4 changes: 2 additions & 2 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package metrics

import (
"errors"
"fmt"

"github.com/kanisterio/errkit"
"github.com/prometheus/client_golang/prometheus"
"gonum.org/v1/gonum/stat/combin"

Expand Down Expand Up @@ -241,7 +241,7 @@ func registerGauge(r prometheus.Registerer, g prometheus.Gauge) prometheus.Gauge
func registerMetricOrDie(r prometheus.Registerer, c prometheus.Collector) prometheus.Collector {
if err := r.Register(c); err != nil {
are := prometheus.AlreadyRegisteredError{}
if !errors.As(err, &are) {
if !errkit.As(err, &are) {
panic(fmt.Sprintf("failed to register metric. error: %v", err))
}
// Use already registered metric
Expand Down
Loading

0 comments on commit 23241c5

Please sign in to comment.