rbd: 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:11:54 -04:00 committed by John Mulligan
parent 1f4543e835
commit 054ae8ddff
4 changed files with 83 additions and 63 deletions

63
rbd/errors.go Normal file
View File

@ -0,0 +1,63 @@
package rbd
/*
#include <errno.h>
*/
import "C"
import (
"errors"
"fmt"
"github.com/ceph/go-ceph/internal/errutil"
)
// revive:disable:exported Temporarily live with stuttering
// RBDError represents an error condition returned from the librbd APIs.
type RBDError int
// revive:enable:exported
func (e RBDError) Error() string {
errno, s := errutil.FormatErrno(int(e))
if s == "" {
return fmt.Sprintf("rbd: ret=%d", errno)
}
return fmt.Sprintf("rbd: ret=%d, %s", errno, s)
}
func getError(err C.int) error {
if err != 0 {
if err == -C.ENOENT {
return ErrNotFound
}
return RBDError(err)
} else {
return nil
}
}
// Public go errors:
var (
// ErrNoIOContext may be returned if an api call requires an IOContext and
// it is not provided.
ErrNoIOContext = errors.New("RBD image does not have an IOContext")
// ErrNoName may be returned if an api call requires a name and it is
// not provided.
ErrNoName = errors.New("RBD image does not have a name")
// ErrSnapshotNoName may be returned if an aip call requires a snapshot
// name and it is not provided.
ErrSnapshotNoName = errors.New("RBD snapshot does not have a name")
// ErrImageNotOpen may be returnened if an api call requires an open image handle and one is not provided.
ErrImageNotOpen = errors.New("RBD image not open")
// ErrNotFound may be returned from an api call when the requested item is
// missing.
ErrNotFound = errors.New("RBD image not found")
// revive:disable:exported for compatibility with old versions
RbdErrorImageNotOpen = ErrImageNotOpen
RbdErrorNotFound = ErrNotFound
// revive:enable:exported
)

20
rbd/errors_test.go Normal file
View File

@ -0,0 +1,20 @@
package rbd
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRBDError(t *testing.T) {
err := getError(0)
assert.NoError(t, err)
err = getError(-39) // NOTEMPTY (image still has a snapshot)
assert.Error(t, err)
assert.Equal(t, err.Error(), "rbd: ret=39, Directory not empty")
err = getError(345) // no such errno
assert.Error(t, err)
assert.Equal(t, err.Error(), "rbd: ret=345")
}

View File

@ -13,12 +13,10 @@ import "C"
import (
"bytes"
"errors"
"fmt"
"io"
"time"
"unsafe"
"github.com/ceph/go-ceph/internal/errutil"
"github.com/ceph/go-ceph/rados"
)
@ -45,35 +43,6 @@ const (
NoSnapshot = ""
)
// revive:disable:exported Temporarily live with stuttering
// RBDError represents an error condition returned from the librbd APIs.
type RBDError int
// revive:enable:exported
var (
// ErrNoIOContext may be returned if an api call requires an IOContext and
// it is not provided.
ErrNoIOContext = errors.New("RBD image does not have an IOContext")
// ErrNoName may be returned if an api call requires a name and it is
// not provided.
ErrNoName = errors.New("RBD image does not have a name")
// ErrSnapshotNoName may be returned if an aip call requires a snapshot
// name and it is not provided.
ErrSnapshotNoName = errors.New("RBD snapshot does not have a name")
// ErrImageNotOpen may be returnened if an api call requires an open image handle and one is not provided.
ErrImageNotOpen = errors.New("RBD image not open")
// ErrNotFound may be returned from an api call when the requested item is
// missing.
ErrNotFound = errors.New("RBD image not found")
// revive:disable:exported for compatibility with old versions
RbdErrorImageNotOpen = ErrImageNotOpen
RbdErrorNotFound = ErrNotFound
// revive:enable:exported
)
// ImageInfo represents the status information for an image.
type ImageInfo struct {
Size uint64
@ -156,25 +125,6 @@ func (image *Image) validate(req uint32) error {
return nil
}
func (e RBDError) Error() string {
errno, s := errutil.FormatErrno(int(e))
if s == "" {
return fmt.Sprintf("rbd: ret=%d", errno)
}
return fmt.Sprintf("rbd: ret=%d, %s", errno, s)
}
func getError(err C.int) error {
if err != 0 {
if err == -C.ENOENT {
return ErrNotFound
}
return RBDError(err)
} else {
return nil
}
}
// Version returns the major, minor, and patch level of the librbd library.
func Version() (int, int, int) {
var c_major, c_minor, c_patch C.int

View File

@ -24,19 +24,6 @@ func GetUUID() string {
return uuid.Must(uuid.NewV4()).String()
}
func TestRBDError(t *testing.T) {
err := getError(0)
assert.NoError(t, err)
err = getError(-39) // NOTEMPTY (image still has a snapshot)
assert.Error(t, err)
assert.Equal(t, err.Error(), "rbd: ret=39, Directory not empty")
err = getError(345) // no such errno
assert.Error(t, err)
assert.Equal(t, err.Error(), "rbd: ret=345")
}
func TestVersion(t *testing.T) {
var major, minor, patch = Version()
assert.False(t, major < 0 || major > 1000, "invalid major")