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:
Sven Anderson
2020-07-09 01:00:23 +02:00
committed by mergify[bot]
parent c277ed588d
commit 8393fbe40b
8 changed files with 74 additions and 145 deletions

View File

@ -37,7 +37,7 @@ func getOMapValues(
// fetch and configure the rados ioctx
ioctx, err := conn.conn.GetIoctx(poolName)
if err != nil {
return nil, omapPoolError(poolName, err)
return nil, omapPoolError(err)
}
defer ioctx.Destroy()
@ -66,7 +66,7 @@ func getOMapValues(
klog.Errorf(
util.Log(ctx, "omap not found (pool=%q, namespace=%q, name=%q): %v"),
poolName, namespace, oid, err)
return nil, util.NewErrKeyNotFound(oid, err)
return nil, util.JoinErrors(util.ErrKeyNotFound, err)
}
return nil, err
}
@ -83,7 +83,7 @@ func removeMapKeys(
// fetch and configure the rados ioctx
ioctx, err := conn.conn.GetIoctx(poolName)
if err != nil {
return omapPoolError(poolName, err)
return omapPoolError(err)
}
defer ioctx.Destroy()
@ -118,7 +118,7 @@ func setOMapKeys(
// fetch and configure the rados ioctx
ioctx, err := conn.conn.GetIoctx(poolName)
if err != nil {
return omapPoolError(poolName, err)
return omapPoolError(err)
}
defer ioctx.Destroy()
@ -142,9 +142,9 @@ func setOMapKeys(
return nil
}
func omapPoolError(poolName string, err error) error {
func omapPoolError(err error) error {
if errors.Is(err, rados.ErrNotFound) {
return util.NewErrPoolNotFound(poolName, err)
return util.JoinErrors(util.ErrPoolNotFound, err)
}
return err
}

View File

@ -282,9 +282,7 @@ func (conn *Connection) CheckReservation(ctx context.Context,
ctx, conn, journalPool, cj.namespace, cj.csiDirectory,
cj.commonPrefix, fetchKeys)
if err != nil {
var eknf util.ErrKeyNotFound
var epnf util.ErrPoolNotFound
if errors.As(err, &eknf) || errors.As(err, &epnf) {
if errors.Is(err, util.ErrKeyNotFound) || errors.Is(err, util.ErrPoolNotFound) {
// pool or omap (oid) was not present
// stop processing but without an error for no reservation exists
return nil, nil
@ -316,8 +314,7 @@ func (conn *Connection) CheckReservation(ctx context.Context,
savedImagePool, err = util.GetPoolName(conn.monitors, conn.cr, savedImagePoolID)
if err != nil {
var epnf util.ErrPoolNotFound
if errors.As(err, &epnf) {
if errors.Is(err, util.ErrPoolNotFound) {
err = conn.UndoReservation(ctx, journalPool, "", "", reqName)
}
return nil, err
@ -329,8 +326,7 @@ func (conn *Connection) CheckReservation(ctx context.Context,
if err != nil {
// error should specifically be not found, for image to be absent, any other error
// is not conclusive, and we should not proceed
var eknf util.ErrKeyNotFound
if errors.As(err, &eknf) {
if errors.Is(err, util.ErrKeyNotFound) {
err = conn.UndoReservation(ctx, journalPool, savedImagePool,
cj.GetNameForUUID(namePrefix, objUUID, snapSource), reqName)
}
@ -363,10 +359,10 @@ func (conn *Connection) CheckReservation(ctx context.Context,
if savedImageAttributes.SourceName != parentName {
// NOTE: This can happen if there is a snapname conflict, and we already have a snapshot
// with the same name pointing to a different UUID as the source
err = fmt.Errorf("snapname points to different volume, request name (%s)"+
" source name (%s) saved source name (%s)",
err = fmt.Errorf("%w: snapname points to different volume, request name (%s)"+
" source name (%s) saved source name (%s)", util.ErrSnapNameConflict,
reqName, parentName, savedImageAttributes.SourceName)
return nil, util.NewErrSnapNameConflict(reqName, err)
return nil, err
}
}
@ -412,8 +408,7 @@ func (conn *Connection) UndoReservation(ctx context.Context,
err := util.RemoveObject(ctx, conn.monitors, conn.cr, volJournalPool, cj.namespace, cj.cephUUIDDirectoryPrefix+imageUUID)
if err != nil {
var eonf util.ErrObjectNotFound
if !errors.As(err, &eonf) {
if !errors.Is(err, util.ErrObjectNotFound) {
klog.Errorf(util.Log(ctx, "failed removing oMap %s (%s)"), cj.cephUUIDDirectoryPrefix+imageUUID, err)
return err
}
@ -445,8 +440,7 @@ func reserveOMapName(ctx context.Context, monitors string, cr *util.Credentials,
err := util.CreateObject(ctx, monitors, cr, pool, namespace, oMapNamePrefix+iterUUID)
if err != nil {
var eoe util.ErrObjectExists
if errors.As(err, &eoe) {
if errors.Is(err, util.ErrObjectExists) {
attempt++
// try again with a different uuid, for maxAttempts tries
util.DebugLog(ctx, "uuid (%s) conflict detected, retrying (attempt %d of %d)",
@ -616,9 +610,7 @@ func (conn *Connection) GetImageAttributes(ctx context.Context, pool, objectUUID
ctx, conn, pool, cj.namespace, cj.cephUUIDDirectoryPrefix+objectUUID,
cj.commonPrefix, fetchKeys)
if err != nil {
var eknf util.ErrKeyNotFound
var epnf util.ErrPoolNotFound
if !errors.As(err, &eknf) && !errors.As(err, &epnf) {
if !errors.Is(err, util.ErrKeyNotFound) && !errors.Is(err, util.ErrPoolNotFound) {
return nil, err
}
klog.Warningf(util.Log(ctx, "unable to read omap keys: pool or key missing: %v"), err)
@ -656,9 +648,8 @@ func (conn *Connection) GetImageAttributes(ctx context.Context, pool, objectUUID
if snapSource {
imageAttributes.SourceName, found = values[cj.cephSnapSourceKey]
if !found {
return nil, util.NewErrKeyNotFound(
cj.cephSnapSourceKey,
fmt.Errorf("no snap source in omap for %q", cj.cephUUIDDirectoryPrefix+objectUUID))
return nil, fmt.Errorf("%w: no snap source in omap for %q",
util.ErrKeyNotFound, cj.cephUUIDDirectoryPrefix+objectUUID)
}
}