rbd: return more useful error messages

Translate the return value (errno) from the C rbd_*() functions into the
standardized strerror() error message.

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2019-12-09 19:43:49 +01:00 committed by John Mulligan
parent dcfe969b09
commit a713db983a
2 changed files with 29 additions and 1 deletions

View File

@ -1,6 +1,9 @@
package rbd
// #cgo LDFLAGS: -lrbd
// /* force XSI-complaint strerror_r() */
// #define _POSIX_C_SOURCE 200112L
// #undef _GNU_SOURCE
// #include <errno.h>
// #include <stdlib.h>
// #include <rados/librados.h>
@ -162,7 +165,19 @@ func (snapshot *Snapshot) validate(req uint32) error {
//
func (e RBDError) Error() string {
return fmt.Sprintf("rbd: ret=%d", e)
buf := make([]byte, 1024)
// strerror expects errno >= 0
errno := e
if errno < 0 {
errno = -errno
}
ret := C.strerror_r(C.int(errno), (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
if ret != 0 {
return fmt.Sprintf("rbd: ret=%d", e)
}
return fmt.Sprintf("rbd: ret=%d, %s", e, C.GoString((*C.char)(unsafe.Pointer(&buf[0]))))
}
//

View File

@ -22,6 +22,19 @@ func GetUUID() string {
return uuid.Must(uuid.NewV4()).String()
}
func TestRBDError(t *testing.T) {
err := rbd.GetError(0)
assert.NoError(t, err)
err = rbd.GetError(-39) // NOTEMPTY (image still has a snapshot)
assert.Error(t, err)
assert.Equal(t, err.Error(), "rbd: ret=-39, Directory not empty")
err = rbd.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 = rbd.Version()
assert.False(t, major < 0 || major > 1000, "invalid major")