From f584aef60fc828c79d3067350f0e193a85202d15 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Mon, 30 Mar 2020 16:22:07 -0400 Subject: [PATCH] cephfs: consolidate error types & values in an errors.go file Signed-off-by: John Mulligan --- cephfs/cephfs.go | 25 ------------------------- cephfs/cephfs_test.go | 13 ------------- cephfs/errors.go | 35 +++++++++++++++++++++++++++++++++++ cephfs/errors_test.go | 20 ++++++++++++++++++++ 4 files changed, 55 insertions(+), 38 deletions(-) create mode 100644 cephfs/errors.go create mode 100644 cephfs/errors_test.go diff --git a/cephfs/cephfs.go b/cephfs/cephfs.go index 03727d3..689d9d9 100644 --- a/cephfs/cephfs.go +++ b/cephfs/cephfs.go @@ -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 diff --git a/cephfs/cephfs_test.go b/cephfs/cephfs_test.go index 1947735..6e82dfc 100644 --- a/cephfs/cephfs_test.go +++ b/cephfs/cephfs_test.go @@ -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) diff --git a/cephfs/errors.go b/cephfs/errors.go new file mode 100644 index 0000000..a28573e --- /dev/null +++ b/cephfs/errors.go @@ -0,0 +1,35 @@ +package cephfs + +/* +#include +*/ +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) +} diff --git a/cephfs/errors_test.go b/cephfs/errors_test.go new file mode 100644 index 0000000..147d38e --- /dev/null +++ b/cephfs/errors_test.go @@ -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") +}