rados: replace radosError by cephError

Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
Niels de Vos 2024-10-02 15:49:43 +02:00 committed by mergify[bot]
parent e2ab47085a
commit 7154dac048
3 changed files with 43 additions and 65 deletions

View File

@ -291,11 +291,11 @@ func (c *Conn) GetPoolByName(name string) (int64, error) {
}
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
ret := int64(C.rados_pool_lookup(c.cluster, cName))
ret := C.rados_pool_lookup(c.cluster, cName)
if ret < 0 {
return 0, radosError(ret)
return 0, getError(C.int(ret))
}
return ret, nil
return int64(ret), nil
}
// GetPoolByID returns the name of a pool by a given ID.
@ -305,9 +305,9 @@ func (c *Conn) GetPoolByID(id int64) (string, error) {
return "", err
}
cid := C.int64_t(id)
ret := int(C.rados_pool_reverse_lookup(c.cluster, cid, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))))
ret := C.rados_pool_reverse_lookup(c.cluster, cid, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
if ret < 0 {
return "", radosError(ret)
return "", getError(ret)
}
return C.GoString((*C.char)(unsafe.Pointer(&buf[0]))), nil
}

View File

@ -11,23 +11,46 @@ import (
"github.com/ceph/go-ceph/internal/errutil"
)
// radosError represents an error condition returned from the Ceph RADOS APIs.
type radosError int
// Public go errors:
// Error returns the error string for the radosError type.
func (e radosError) Error() string {
return errutil.FormatErrorCode("rados", int(e))
}
var (
// ErrNotConnected is returned when functions are called
// without a RADOS connection.
ErrNotConnected = getError(-C.ENOTCONN)
// ErrEmptyArgument may be returned if a function argument is passed
// a zero-length slice or map.
ErrEmptyArgument = errors.New("Argument must contain at least one item")
// ErrInvalidIOContext may be returned if an api call requires an IOContext
// but IOContext is not ready for use.
ErrInvalidIOContext = errors.New("IOContext is not ready for use")
// ErrOperationIncomplete is returned from write op or read op steps for
// which the operation has not been performed yet.
ErrOperationIncomplete = errors.New("Operation has not been performed yet")
func (e radosError) ErrorCode() int {
return int(e)
}
// ErrNotFound indicates a missing resource.
ErrNotFound = getError(-C.ENOENT)
// ErrPermissionDenied indicates a permissions issue.
ErrPermissionDenied = getError(-C.EPERM)
// ErrObjectExists indicates that an exclusive object creation failed.
ErrObjectExists = getError(-C.EEXIST)
func getError(e C.int) error {
if e == 0 {
return nil
}
return radosError(e)
// RadosErrorNotFound indicates a missing resource.
//
// Deprecated: use ErrNotFound instead
RadosErrorNotFound = ErrNotFound
// RadosErrorPermissionDenied indicates a permissions issue.
//
// Deprecated: use ErrPermissionDenied instead
RadosErrorPermissionDenied = ErrPermissionDenied
// Private errors:
errNameTooLong = getError(-C.ENAMETOOLONG)
errRange = getError(-C.ERANGE)
)
func getError(errno C.int) error {
return errutil.GetError("rados", int(errno))
}
// getErrorIfNegative converts a ceph return code to error if negative.
@ -39,48 +62,3 @@ func getErrorIfNegative(ret C.int) error {
}
return getError(ret)
}
// Public go errors:
var (
// ErrNotConnected is returned when functions are called
// without a RADOS connection.
ErrNotConnected = errors.New("RADOS not connected")
// ErrEmptyArgument may be returned if a function argument is passed
// a zero-length slice or map.
ErrEmptyArgument = errors.New("Argument must contain at least one item")
// ErrInvalidIOContext may be returned if an api call requires an IOContext
// but IOContext is not ready for use.
ErrInvalidIOContext = errors.New("IOContext is not ready for use")
// ErrOperationIncomplete is returned from write op or read op steps for
// which the operation has not been performed yet.
ErrOperationIncomplete = errors.New("Operation has not been performed yet")
)
// Public radosErrors:
const (
// ErrNotFound indicates a missing resource.
ErrNotFound = radosError(-C.ENOENT)
// ErrPermissionDenied indicates a permissions issue.
ErrPermissionDenied = radosError(-C.EPERM)
// ErrObjectExists indicates that an exclusive object creation failed.
ErrObjectExists = radosError(-C.EEXIST)
// RadosErrorNotFound indicates a missing resource.
//
// Deprecated: use ErrNotFound instead
RadosErrorNotFound = ErrNotFound
// RadosErrorPermissionDenied indicates a permissions issue.
//
// Deprecated: use ErrPermissionDenied instead
RadosErrorPermissionDenied = ErrPermissionDenied
)
// Private errors:
const (
errNameTooLong = radosError(-C.ENAMETOOLONG)
errRange = radosError(-C.ERANGE)
)

View File

@ -640,7 +640,7 @@ func (ioctx *IOContext) ListLockers(oid, name string) (*LockInfo, error) {
}
if ret < 0 {
return nil, radosError(ret)
return nil, getError(C.int(ret))
}
return &LockInfo{int(ret), cExclusive == 1, C.GoString(cTag), splitCString(cClients, cClientsLen), splitCString(cCookies, cCookiesLen), splitCString(cAddrs, cAddrsLen)}, nil
}