mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 02:33:34 +00:00
util: simplify error handling
The sentinel error code had additional fields in the errors, that are used nowhere. This leads to unneccesarily complicated code. This change replaces the sentinel errors in utils with standard errors created with errors.New() and adds a simple JoinErrors() function to be able to combine sentinel errors from different code tiers. Related: #1203 Signed-off-by: Sven Anderson <sven@redhat.com>
This commit is contained in:
committed by
mergify[bot]
parent
c277ed588d
commit
8393fbe40b
@ -61,7 +61,8 @@ func GetPoolID(monitors string, cr *Credentials, poolName string) (int64, error)
|
||||
|
||||
id, err := conn.GetPoolByName(poolName)
|
||||
if errors.Is(err, rados.ErrNotFound) {
|
||||
return InvalidPoolID, ErrPoolNotFound{poolName, fmt.Errorf("pool (%s) not found in Ceph cluster", poolName)}
|
||||
return InvalidPoolID, fmt.Errorf("%w: pool (%s) not found in Ceph cluster",
|
||||
ErrPoolNotFound, poolName)
|
||||
} else if err != nil {
|
||||
return InvalidPoolID, err
|
||||
}
|
||||
@ -80,7 +81,8 @@ func GetPoolName(monitors string, cr *Credentials, poolID int64) (string, error)
|
||||
|
||||
name, err := conn.GetPoolByID(poolID)
|
||||
if err != nil {
|
||||
return "", ErrPoolNotFound{string(poolID), fmt.Errorf("pool ID (%d) not found in Ceph cluster", poolID)}
|
||||
return "", fmt.Errorf("%w: pool ID (%d) not found in Ceph cluster",
|
||||
ErrPoolNotFound, poolID)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
@ -117,9 +119,8 @@ func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolNam
|
||||
|
||||
ioctx, err := conn.GetIoctx(poolName)
|
||||
if err != nil {
|
||||
var epnf ErrPoolNotFound
|
||||
if errors.As(err, &epnf) {
|
||||
err = ErrObjectNotFound{poolName, err}
|
||||
if errors.Is(err, ErrPoolNotFound) {
|
||||
err = JoinErrors(ErrObjectNotFound, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@ -131,7 +132,7 @@ func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolNam
|
||||
|
||||
err = ioctx.Create(objectName, rados.CreateExclusive)
|
||||
if errors.Is(err, rados.ErrObjectExists) {
|
||||
return ErrObjectExists{objectName, err}
|
||||
return JoinErrors(ErrObjectExists, err)
|
||||
} else if err != nil {
|
||||
klog.Errorf(Log(ctx, "failed creating omap (%s) in pool (%s): (%v)"), objectName, poolName, err)
|
||||
return err
|
||||
@ -152,9 +153,8 @@ func RemoveObject(ctx context.Context, monitors string, cr *Credentials, poolNam
|
||||
|
||||
ioctx, err := conn.GetIoctx(poolName)
|
||||
if err != nil {
|
||||
var epnf ErrPoolNotFound
|
||||
if errors.As(err, &epnf) {
|
||||
err = ErrObjectNotFound{poolName, err}
|
||||
if errors.Is(err, ErrPoolNotFound) {
|
||||
err = JoinErrors(ErrObjectNotFound, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@ -166,7 +166,7 @@ func RemoveObject(ctx context.Context, monitors string, cr *Credentials, poolNam
|
||||
|
||||
err = ioctx.Delete(oMapName)
|
||||
if errors.Is(err, rados.ErrNotFound) {
|
||||
return ErrObjectNotFound{oMapName, err}
|
||||
return JoinErrors(ErrObjectNotFound, err)
|
||||
} else if err != nil {
|
||||
klog.Errorf(Log(ctx, "failed removing omap (%s) in pool (%s): (%v)"), oMapName, poolName, err)
|
||||
return err
|
||||
|
@ -74,7 +74,7 @@ func (cc *ClusterConnection) GetIoctx(pool string) (*rados.IOContext, error) {
|
||||
if err != nil {
|
||||
// ErrNotFound indicates the Pool was not found
|
||||
if errors.Is(err, rados.ErrNotFound) {
|
||||
err = ErrPoolNotFound{pool, err}
|
||||
err = JoinErrors(ErrPoolNotFound, err)
|
||||
} else {
|
||||
err = fmt.Errorf("failed to open IOContext for pool %s: %w", pool, err)
|
||||
}
|
||||
|
@ -16,98 +16,46 @@ limitations under the License.
|
||||
|
||||
package util
|
||||
|
||||
// ErrKeyNotFound is returned when requested key in omap is not found.
|
||||
type ErrKeyNotFound struct {
|
||||
keyName string
|
||||
err error
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrKeyNotFound is returned when requested key in omap is not found.
|
||||
ErrKeyNotFound = errors.New("key not found")
|
||||
// ErrObjectExists is returned when named omap is already present in rados.
|
||||
ErrObjectExists = errors.New("object exists")
|
||||
// ErrObjectNotFound is returned when named omap is not found in rados.
|
||||
ErrObjectNotFound = errors.New("object not found")
|
||||
// ErrSnapNameConflict is generated when a requested CSI snap name already exists on RBD but with
|
||||
// different properties, and hence is in conflict with the passed in CSI volume name.
|
||||
ErrSnapNameConflict = errors.New("snapshot name conflict")
|
||||
// ErrPoolNotFound is returned when pool is not found.
|
||||
ErrPoolNotFound = errors.New("pool not found")
|
||||
)
|
||||
|
||||
type errorPair struct {
|
||||
first error
|
||||
second error
|
||||
}
|
||||
|
||||
// NewErrKeyNotFound returns a new ErrKeyNotFound error.
|
||||
func NewErrKeyNotFound(keyName string, err error) ErrKeyNotFound {
|
||||
return ErrKeyNotFound{keyName, err}
|
||||
func (e errorPair) Error() string {
|
||||
return fmt.Sprintf("%v: %v", e.first, e.second)
|
||||
}
|
||||
|
||||
// Error returns the error string for ErrKeyNotFound.
|
||||
func (e ErrKeyNotFound) Error() string {
|
||||
return e.err.Error()
|
||||
// Is checks if target error is wrapped in the first error.
|
||||
func (e errorPair) Is(target error) bool {
|
||||
return errors.Is(e.first, target)
|
||||
}
|
||||
|
||||
// Unwrap returns the encapsulated error.
|
||||
func (e ErrKeyNotFound) Unwrap() error {
|
||||
return e.err
|
||||
// Unwrap returns the second error.
|
||||
func (e errorPair) Unwrap() error {
|
||||
return e.second
|
||||
}
|
||||
|
||||
// ErrObjectExists is returned when named omap is already present in rados.
|
||||
type ErrObjectExists struct {
|
||||
objectName string
|
||||
err error
|
||||
}
|
||||
|
||||
// Error returns the error string for ErrObjectExists.
|
||||
func (e ErrObjectExists) Error() string {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
// Unwrap returns the encapsulated error.
|
||||
func (e ErrObjectExists) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
// ErrObjectNotFound is returned when named omap is not found in rados.
|
||||
type ErrObjectNotFound struct {
|
||||
oMapName string
|
||||
err error
|
||||
}
|
||||
|
||||
// Error returns the error string for ErrObjectNotFound.
|
||||
func (e ErrObjectNotFound) Error() string {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
// Unwrap returns the encapsulated error.
|
||||
func (e ErrObjectNotFound) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
// ErrSnapNameConflict is generated when a requested CSI snap name already exists on RBD but with
|
||||
// different properties, and hence is in conflict with the passed in CSI volume name.
|
||||
type ErrSnapNameConflict struct {
|
||||
requestName string
|
||||
err error
|
||||
}
|
||||
|
||||
// Error returns the error string for ErrSnapNameConflict.
|
||||
func (e ErrSnapNameConflict) Error() string {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
// Unwrap returns the encapsulated error.
|
||||
func (e ErrSnapNameConflict) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
// NewErrSnapNameConflict returns a ErrSnapNameConflict error when CSI snap name already exists.
|
||||
func NewErrSnapNameConflict(name string, err error) ErrSnapNameConflict {
|
||||
return ErrSnapNameConflict{name, err}
|
||||
}
|
||||
|
||||
// ErrPoolNotFound is returned when pool is not found.
|
||||
type ErrPoolNotFound struct {
|
||||
Pool string
|
||||
Err error
|
||||
}
|
||||
|
||||
// Error returns the error string for ErrPoolNotFound.
|
||||
func (e ErrPoolNotFound) Error() string {
|
||||
return e.Err.Error()
|
||||
}
|
||||
|
||||
// Unwrap returns the encapsulated error.
|
||||
func (e ErrPoolNotFound) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
// NewErrPoolNotFound returns a new ErrPoolNotFound error.
|
||||
func NewErrPoolNotFound(pool string, err error) ErrPoolNotFound {
|
||||
return ErrPoolNotFound{pool, err}
|
||||
// JoinErrors combines two errors. Of the returned error, Is() follows the first
|
||||
// branch, Unwrap() folllows the second branch.
|
||||
func JoinErrors(e1, e2 error) error {
|
||||
return errorPair{e1, e2}
|
||||
}
|
||||
|
Reference in New Issue
Block a user