cephfs: 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:22:07 -04:00 committed by John Mulligan
parent 054ae8ddff
commit f584aef60f
4 changed files with 55 additions and 38 deletions

View File

@ -9,36 +9,11 @@ package cephfs
import "C"
import (
"fmt"
"unsafe"
"github.com/ceph/go-ceph/internal/errutil"
"github.com/ceph/go-ceph/rados"
)
// revive:disable:exported Temporarily live with stuttering
// CephFSError represents an error condition returned from the CephFS APIs.
type CephFSError int
// revive:enable:exported
// Error returns the error string for the CephFSError type.
func (e CephFSError) Error() string {
errno, s := errutil.FormatErrno(int(e))
if s == "" {
return fmt.Sprintf("cephfs: ret=%d", errno)
}
return fmt.Sprintf("cephfs: ret=%d, %s", errno, s)
}
func getError(e C.int) error {
if e == 0 {
return nil
}
return CephFSError(e)
}
// MountInfo exports ceph's ceph_mount_info from libcephfs.cc
type MountInfo struct {
mount *C.struct_ceph_mount_info

View File

@ -256,19 +256,6 @@ func TestChown(t *testing.T) {
}
func TestCephFSError(t *testing.T) {
err := getError(0)
assert.NoError(t, err)
err = getError(-5) // IO error
assert.Error(t, err)
assert.Equal(t, err.Error(), "cephfs: ret=5, Input/output error")
err = getError(345) // no such errno
assert.Error(t, err)
assert.Equal(t, err.Error(), "cephfs: ret=345")
}
func radosConnect(t *testing.T) *rados.Conn {
conn, err := rados.NewConn()
require.NoError(t, err)

35
cephfs/errors.go Normal file
View File

@ -0,0 +1,35 @@
package cephfs
/*
#include <errno.h>
*/
import "C"
import (
"fmt"
"github.com/ceph/go-ceph/internal/errutil"
)
// revive:disable:exported Temporarily live with stuttering
// CephFSError represents an error condition returned from the CephFS APIs.
type CephFSError int
// revive:enable:exported
// Error returns the error string for the CephFSError type.
func (e CephFSError) Error() string {
errno, s := errutil.FormatErrno(int(e))
if s == "" {
return fmt.Sprintf("cephfs: ret=%d", errno)
}
return fmt.Sprintf("cephfs: ret=%d, %s", errno, s)
}
func getError(e C.int) error {
if e == 0 {
return nil
}
return CephFSError(e)
}

20
cephfs/errors_test.go Normal file
View File

@ -0,0 +1,20 @@
package cephfs
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCephFSError(t *testing.T) {
err := getError(0)
assert.NoError(t, err)
err = getError(-5) // IO error
assert.Error(t, err)
assert.Equal(t, err.Error(), "cephfs: ret=5, Input/output error")
err = getError(345) // no such errno
assert.Error(t, err)
assert.Equal(t, err.Error(), "cephfs: ret=345")
}