diff --git a/rados/conn.go b/rados/conn.go index 78c3b26..617fb68 100644 --- a/rados/conn.go +++ b/rados/conn.go @@ -7,15 +7,9 @@ import "C" import ( "bytes" - "errors" "unsafe" ) -var ( - // ErrNotConnected is returned when functions are called without a RADOS connection - ErrNotConnected = errors.New("RADOS not connected") -) - // ClusterStat represents Ceph cluster statistics. type ClusterStat struct { Kb uint64 diff --git a/rados/errors.go b/rados/errors.go new file mode 100644 index 0000000..0deb60b --- /dev/null +++ b/rados/errors.go @@ -0,0 +1,63 @@ +package rados + +/* +#include +*/ +import "C" + +import ( + "errors" + "fmt" + + "github.com/ceph/go-ceph/internal/errutil" +) + +// revive:disable:exported Temporarily live with stuttering + +// RadosError represents an error condition returned from the Ceph RADOS APIs. +type RadosError int + +// revive:enable:exported + +// Error returns the error string for the RadosError type. +func (e RadosError) Error() string { + errno, s := errutil.FormatErrno(int(e)) + if s == "" { + return fmt.Sprintf("rados: ret=%d", errno) + } + return fmt.Sprintf("rados: ret=%d, %s", errno, s) +} + +func getRadosError(err int) error { + if err == 0 { + return nil + } + return RadosError(err) +} + +// Public go errors: + +var ( + // ErrNotConnected is returned when functions are called without a RADOS connection + ErrNotConnected = errors.New("RADOS not connected") +) + +// 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 +) diff --git a/rados/errors_test.go b/rados/errors_test.go new file mode 100644 index 0000000..8b2d802 --- /dev/null +++ b/rados/errors_test.go @@ -0,0 +1,20 @@ +package rados + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRadosError(t *testing.T) { + err := getRadosError(0) + assert.NoError(t, err) + + err = getRadosError(-5) // IO error + assert.Error(t, err) + assert.Equal(t, err.Error(), "rados: ret=5, Input/output error") + + err = getRadosError(345) // no such errno + assert.Error(t, err) + assert.Equal(t, err.Error(), "rados: ret=345") +} diff --git a/rados/rados.go b/rados/rados.go index 008db3c..ea1cae2 100644 --- a/rados/rados.go +++ b/rados/rados.go @@ -7,41 +7,15 @@ package rados import "C" import ( - "fmt" "runtime" "unsafe" - - "github.com/ceph/go-ceph/internal/errutil" ) -// revive:disable:exported Temporarily live with stuttering - -// RadosError represents an error condition returned from the Ceph RADOS APIs. -type RadosError int - -// revive:enable:exported - -// Error returns the error string for the RadosError type. -func (e RadosError) Error() string { - errno, s := errutil.FormatErrno(int(e)) - if s == "" { - return fmt.Sprintf("rados: ret=%d", errno) - } - return fmt.Sprintf("rados: ret=%d, %s", errno, s) -} - const ( // AllNamespaces is used to reset a selected namespace to all // namespaces. See the IOContext SetNamespace function. AllNamespaces = C.LIBRADOS_ALL_NSPACES - // 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) - // FIXME: for backwards compatibility // RadosAllNamespaces is used to reset a selected namespace to all @@ -49,23 +23,8 @@ const ( // // Deprecated: use AllNamespaces instead RadosAllNamespaces = AllNamespaces - // RadosErrorNotFound indicates a missing resource. - // - // Deprecated: use ErrNotFound instead - RadosErrorNotFound = ErrNotFound - // RadosErrorPermissionDenied indicates a permissions issue. - // - // Deprecated: use ErrPermissionDenied instead - RadosErrorPermissionDenied = ErrPermissionDenied ) -func getRadosError(err int) error { - if err == 0 { - return nil - } - return RadosError(err) -} - // Version returns the major, minor, and patch components of the version of // the RADOS library linked against. func Version() (int, int, int) { diff --git a/rados/rados_test.go b/rados/rados_test.go index e7be822..85c8ef0 100644 --- a/rados/rados_test.go +++ b/rados/rados_test.go @@ -1200,16 +1200,3 @@ func (suite *RadosTestSuite) TestOpenIOContextInvalidPool() { func TestRadosTestSuite(t *testing.T) { suite.Run(t, new(RadosTestSuite)) } - -func TestRadosError(t *testing.T) { - err := getRadosError(0) - assert.NoError(t, err) - - err = getRadosError(-5) // IO error - assert.Error(t, err) - assert.Equal(t, err.Error(), "rados: ret=5, Input/output error") - - err = getRadosError(345) // no such errno - assert.Error(t, err) - assert.Equal(t, err.Error(), "rados: ret=345") -}