rados: consolidate error types & values in an errors.go file

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-03-30 16:02:07 -04:00 committed by John Mulligan
parent a5e5ae6206
commit 1f4543e835
5 changed files with 83 additions and 60 deletions

View File

@ -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

63
rados/errors.go Normal file
View File

@ -0,0 +1,63 @@
package rados
/*
#include <errno.h>
*/
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
)

20
rados/errors_test.go Normal file
View File

@ -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")
}

View File

@ -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) {

View File

@ -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")
}