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 21, 2024
1 parent fee393d commit 0ad85f4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion 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 Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ github.com/kanisterio/errkit v0.0.2 h1:3v3HGz9lHIbZR6Jr9qIJpRjaqUX0rsJSLMEQGsMHi
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/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
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)
}
}

0 comments on commit 0ad85f4

Please sign in to comment.