cephfs: simplify error handling

This change replaces the sentinel errors in cephfs module with
standard errors created with errors.New().

Related: #1203

Signed-off-by: Sven Anderson <sven@redhat.com>
This commit is contained in:
Sven Anderson 2020-07-10 02:14:39 +02:00 committed by mergify[bot]
parent 7c9c7c78a7
commit dba2c27bcb
6 changed files with 22 additions and 61 deletions

View File

@ -184,8 +184,7 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
}
// All errors other than ErrVolumeNotFound should return an error back to the caller
var evnf ErrVolumeNotFound
if !errors.As(err, &evnf) {
if !errors.Is(err, ErrVolumeNotFound) {
return nil, status.Error(codes.Internal, err.Error())
}
@ -221,8 +220,7 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
if err = purgeVolume(ctx, volumeID(vID.FsSubvolName), cr, volOptions); err != nil {
klog.Errorf(util.Log(ctx, "failed to delete volume %s: %v"), volID, err)
// All errors other than ErrVolumeNotFound should return an error back to the caller
var evnf ErrVolumeNotFound
if !errors.As(err, &evnf) {
if !errors.Is(err, ErrVolumeNotFound) {
return nil, status.Error(codes.Internal, err.Error())
}
}

View File

@ -16,49 +16,15 @@ limitations under the License.
package cephfs
import "errors"
var (
// ErrInvalidVolID is returned when a CSI passed VolumeID is not conformant to any known volume ID
// formats.
type ErrInvalidVolID struct {
err error
}
// Error returns a user presentable string of the error.
func (e ErrInvalidVolID) Error() string {
return e.err.Error()
}
// Unwrap returns the encapsulated error of ErrInvalidVolID.
func (e ErrInvalidVolID) Unwrap() error {
return e.err
}
ErrInvalidVolID = errors.New("invalid VolumeID")
// ErrNonStaticVolume is returned when a volume is detected as not being
// statically provisioned.
type ErrNonStaticVolume struct {
err error
}
// Error returns a user presentable string of the error.
func (e ErrNonStaticVolume) Error() string {
return e.err.Error()
}
// Unwrap returns the encapsulated error of ErrNonStaticVolume.
func (e ErrNonStaticVolume) Unwrap() error {
return e.err
}
ErrNonStaticVolume = errors.New("volume not static")
// ErrVolumeNotFound is returned when a subvolume is not found in CephFS.
type ErrVolumeNotFound struct {
err error
}
// Error returns a user presentable string of the error.
func (e ErrVolumeNotFound) Error() string {
return e.err.Error()
}
// Unwrap returns the encapsulated error of ErrVolumeNotFound.
func (e ErrVolumeNotFound) Unwrap() error {
return e.err
}
ErrVolumeNotFound = errors.New("volume not found")
)

View File

@ -72,8 +72,7 @@ func checkVolExists(ctx context.Context, volOptions *volumeOptions, secret map[s
_, err = getVolumeRootPathCeph(ctx, volOptions, cr, volumeID(vid.FsSubvolName))
if err != nil {
var evnf ErrVolumeNotFound
if errors.As(err, &evnf) {
if errors.Is(err, ErrVolumeNotFound) {
err = j.UndoReservation(ctx, volOptions.MetadataPool,
volOptions.MetadataPool, vid.FsSubvolName, volOptions.RequestName)
return nil, err

View File

@ -89,16 +89,14 @@ func (ns *NodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
volOptions, _, err := newVolumeOptionsFromVolID(ctx, string(volID), req.GetVolumeContext(), req.GetSecrets())
if err != nil {
var eivi ErrInvalidVolID
if !errors.As(err, &eivi) {
if !errors.Is(err, ErrInvalidVolID) {
return nil, status.Error(codes.Internal, err.Error())
}
// gets mon IPs from the supplied cluster info
volOptions, _, err = newVolumeOptionsFromStaticVolume(string(volID), req.GetVolumeContext())
if err != nil {
var ensv ErrNonStaticVolume
if !errors.As(err, &ensv) {
if !errors.Is(err, ErrNonStaticVolume) {
return nil, status.Error(codes.Internal, err.Error())
}

View File

@ -70,7 +70,7 @@ func getVolumeRootPathCeph(ctx context.Context, volOptions *volumeOptions, cr *u
klog.Errorf(util.Log(ctx, "failed to get the rootpath for the vol %s(%s) stdError %s"), string(volID), err, stdErrString)
if strings.HasPrefix(stdErrString, errNotFoundString) {
return "", ErrVolumeNotFound{err}
return "", util.JoinErrors(ErrVolumeNotFound, err)
}
return "", err
@ -215,7 +215,7 @@ func purgeVolume(ctx context.Context, volID volumeID, cr *util.Credentials, volO
klog.Errorf(util.Log(ctx, "failed to purge subvolume %s(%s) in fs %s"), string(volID), err, volOptions.FsName)
if strings.HasPrefix(err.Error(), errNotFoundString) {
return ErrVolumeNotFound{err}
return util.JoinErrors(ErrVolumeNotFound, err)
}
return err

View File

@ -230,8 +230,8 @@ func newVolumeOptionsFromVolID(ctx context.Context, volID string, volOpt, secret
// before other errors
err := vi.DecomposeCSIID(volID)
if err != nil {
err = fmt.Errorf("error decoding volume ID (%s) (%s)", err, volID)
return nil, nil, ErrInvalidVolID{err}
err = fmt.Errorf("error decoding volume ID (%s): %w", volID, err)
return nil, nil, util.JoinErrors(ErrInvalidVolID, err)
}
volOptions.ClusterID = vi.ClusterID
vid.VolumeID = volID
@ -372,7 +372,7 @@ func newVolumeOptionsFromStaticVolume(volID string, options map[string]string) (
val, ok := options["staticVolume"]
if !ok {
return nil, nil, ErrNonStaticVolume{err}
return nil, nil, ErrNonStaticVolume
}
if staticVol, err = strconv.ParseBool(val); err != nil {
@ -380,7 +380,7 @@ func newVolumeOptionsFromStaticVolume(volID string, options map[string]string) (
}
if !staticVol {
return nil, nil, ErrNonStaticVolume{err}
return nil, nil, ErrNonStaticVolume
}
// Volume is static, and ProvisionVolume carries bool stating if it was provisioned, hence