]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind: store errno in Error exception also 14497/head
authorKefu Chai <kchai@redhat.com>
Thu, 13 Apr 2017 07:26:14 +0000 (15:26 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 20 Apr 2017 08:32:06 +0000 (16:32 +0800)
also pass the errno and error string to Exception's __init__()
so we can print them all using str(err). otherwise str(err) will only
print out the errorstr, and the type information is lost. for example,
if ping_monitor() raises an exception, all we get is an error of:

  error calling ping_monitor

which is barely helpful. after this change:

  [Errno 22] error calling ping_monitor

is printed instead.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/pybind/cephfs/cephfs.pyx
src/pybind/rados/rados.pyx
src/pybind/rbd/rbd.pyx
src/pybind/rgw/rgw.pyx

index 746b9e8da5733b12037c5b59c88417a7cd1e4cf6..94df1b21a995c91fecad29f71003f8c5fdd7c681 100644 (file)
@@ -151,51 +151,59 @@ class Error(Exception):
     pass
 
 
-class PermissionError(Error):
-    pass
+class OSError(Error):
+    def __init__(self, errno, strerror):
+        self.errno = errno
+        self.strerror = strerror
+
+    def __str__(self):
+        return '[Errno {0}] {1}'.format(self.errno, self.strerror)
 
 
-class ObjectNotFound(Error):
+class PermissionError(OSError):
     pass
 
 
-class NoData(Error):
+class ObjectNotFound(OSError):
     pass
 
 
-class ObjectExists(Error):
+class NoData(OSError):
     pass
 
 
-class IOError(Error):
+class ObjectExists(OSError):
     pass
 
 
-class NoSpace(Error):
+class IOError(OSError):
     pass
 
 
-class InvalidValue(Error):
+class NoSpace(OSError):
     pass
 
 
-class OperationNotSupported(Error):
+class InvalidValue(OSError):
     pass
 
 
-class IncompleteWriteError(Error):
+class OperationNotSupported(OSError):
     pass
 
 
 class LibCephFSStateError(Error):
     pass
 
-class WouldBlock(Error):
+
+class WouldBlock(OSError):
     pass
 
-class OutOfRange(Error):
+
+class OutOfRange(OSError):
     pass
 
+
 IF UNAME_SYSNAME == "FreeBSD":
     cdef errno_to_exception =  {
         errno.EPERM      : PermissionError,
@@ -236,9 +244,9 @@ cdef make_ex(ret, msg):
     """
     ret = abs(ret)
     if ret in errno_to_exception:
-        return errno_to_exception[ret](msg)
+        return errno_to_exception[ret](ret, msg)
     else:
-        return Error(msg + (": error code %d" % ret))
+        return Error(ret, msg + (": error code %d" % ret))
 
 
 class DirEntry(namedtuple('DirEntry',
index 47d362e972ac91ad20f0e7d7451d76c08f37eb89..6c657317d4f0f422c2a8caf9b0b67c70f828a45d 100644 (file)
@@ -295,59 +295,68 @@ LIBRADOS_CREATE_IDEMPOTENT = _LIBRADOS_CREATE_IDEMPOTENT
 ANONYMOUS_AUID = 0xffffffffffffffff
 ADMIN_AUID = 0
 
+
 class Error(Exception):
     """ `Error` class, derived from `Exception` """
+    pass
 
 
 class InvalidArgument(Error):
     pass
 
 
-class InterruptedOrTimeoutError(Error):
-    """ `InterruptedOrTimeoutError` class, derived from `Error` """
-    pass
+class OSError(Error):
+    """ `OSError` class, derived from `Error` """
+    def __init__(self, errno, strerror):
+        self.errno = errno
+        self.strerror = strerror
+
+    def __str__(self):
+        return '[Errno {0}] {1}'.format(self.errno, self.strerror)
 
 
-class PermissionError(Error):
-    """ `PermissionError` class, derived from `Error` """
+class InterruptedOrTimeoutError(OSError):
+    """ `InterruptedOrTimeoutError` class, derived from `OSError` """
     pass
 
-class PermissionDeniedError(Error):
-    """ deal with EACCES related. """
+
+class PermissionError(OSError):
+    """ `PermissionError` class, derived from `OSError` """
     pass
 
-class ObjectNotFound(Error):
-    """ `ObjectNotFound` class, derived from `Error` """
+
+class PermissionDeniedError(OSError):
+    """ deal with EACCES related. """
     pass
 
 
-class NoData(Error):
-    """ `NoData` class, derived from `Error` """
+class ObjectNotFound(OSError):
+    """ `ObjectNotFound` class, derived from `OSError` """
     pass
 
 
-class ObjectExists(Error):
-    """ `ObjectExists` class, derived from `Error` """
+class NoData(OSError):
+    """ `NoData` class, derived from `OSError` """
     pass
 
 
-class ObjectBusy(Error):
-    """ `ObjectBusy` class, derived from `Error` """
+class ObjectExists(OSError):
+    """ `ObjectExists` class, derived from `OSError` """
     pass
 
 
-class IOError(Error):
-    """ `IOError` class, derived from `Error` """
+class ObjectBusy(OSError):
+    """ `ObjectBusy` class, derived from `IOError` """
     pass
 
 
-class NoSpace(Error):
-    """ `NoSpace` class, derived from `Error` """
+class IOError(OSError):
+    """ `ObjectBusy` class, derived from `OSError` """
     pass
 
 
-class IncompleteWriteError(Error):
-    """ `IncompleteWriteError` class, derived from `Error` """
+class NoSpace(OSError):
+    """ `NoSpace` class, derived from `OSError` """
     pass
 
 
@@ -360,6 +369,7 @@ class IoctxStateError(Error):
     """ `IoctxStateError` class, derived from `Error` """
     pass
 
+
 class ObjectStateError(Error):
     """ `ObjectStateError` class, derived from `Error` """
     pass
@@ -370,8 +380,8 @@ class LogicError(Error):
     pass
 
 
-class TimedOut(Error):
-    """ `TimedOut` class, derived from `Error` """
+class TimedOut(OSError):
+    """ `TimedOut` class, derived from `OSError` """
     pass
 
 
@@ -415,9 +425,9 @@ cdef make_ex(ret, msg):
     """
     ret = abs(ret)
     if ret in errno_to_exception:
-        return errno_to_exception[ret](msg)
+        return errno_to_exception[ret](ret, msg)
     else:
-        return Error(msg + (": error code %d" % ret))
+        return Error(ret, msg + (": error code %d" % ret))
 
 
 # helper to specify an optional argument, where in addition to `cls`, `None`
index 08d067bf5347934ce8231ba0110f589dee4e3039..ced4d5d8b1679184c04af1bb9adc1fc50efb0fcc 100644 (file)
@@ -381,31 +381,41 @@ class Error(Exception):
     pass
 
 
-class PermissionError(Error):
+class OSError(Error):
+    """ `OSError` class, derived from `Error` """
+    def __init__(self, errno, strerror):
+        self.errno = errno
+        self.strerror = strerror
+
+    def __str__(self):
+        return '[Errno {0}] {1}'.format(self.errno, self.strerror)
+
+
+class PermissionError(OSError):
     pass
 
 
-class ImageNotFound(Error):
+class ImageNotFound(OSError):
     pass
 
 
-class ImageExists(Error):
+class ImageExists(OSError):
     pass
 
 
-class IOError(Error):
+class IOError(OSError):
     pass
 
 
-class NoSpace(Error):
+class NoSpace(OSError):
     pass
 
 
-class IncompleteWriteError(Error):
+class IncompleteWriteError(OSError):
     pass
 
 
-class InvalidArgument(Error):
+class InvalidArgument(OSError):
     pass
 
 
@@ -413,34 +423,34 @@ class LogicError(Error):
     pass
 
 
-class ReadOnlyImage(Error):
+class ReadOnlyImage(OSError):
     pass
 
 
-class ImageBusy(Error):
+class ImageBusy(OSError):
     pass
 
 
-class ImageHasSnapshots(Error):
+class ImageHasSnapshots(OSError):
     pass
 
 
-class FunctionNotSupported(Error):
+class FunctionNotSupported(OSError):
     pass
 
 
-class ArgumentOutOfRange(Error):
+class ArgumentOutOfRange(OSError):
     pass
 
 
-class ConnectionShutdown(Error):
+class ConnectionShutdown(OSError):
     pass
 
 
-class Timeout(Error):
+class Timeout(OSError):
     pass
 
-class DiskQuotaExceeded(Error):
+class DiskQuotaExceeded(OSError):
     pass
 
 
@@ -473,9 +483,9 @@ cdef make_ex(ret, msg):
     """
     ret = abs(ret)
     if ret in errno_to_exception:
-        return errno_to_exception[ret](msg)
+        return errno_to_exception[ret](ret, msg)
     else:
-        return Error(msg + (": error code %d" % ret))
+        return Error(ret, msg + (": error code %d" % ret))
 
 
 cdef rados_ioctx_t convert_ioctx(rados.Ioctx ioctx) except? NULL:
index b492d70123ad99dcf6a512b51d13a08242f23a76..f512d33f84ab9ed80f86d5caa64067faf0134192 100644 (file)
@@ -183,11 +183,21 @@ class Error(Exception):
     pass
 
 
-class PermissionError(Error):
+class OSError(Error):
+    """ `OSError` class, derived from `Error` """
+    def __init__(self, errno, strerror):
+        self.errno = errno
+        self.strerror = strerror
+
+    def __str__(self):
+        return '[Errno {0}] {1}'.format(self.errno, self.strerror)
+
+
+class PermissionError(OSError):
     pass
 
 
-class ObjectNotFound(Error):
+class ObjectNotFound(OSError):
     pass
 
 
@@ -199,7 +209,7 @@ class ObjectExists(Error):
     pass
 
 
-class IOError(Error):
+class IOError(OSError):
     pass
 
 
@@ -299,7 +309,7 @@ cdef make_ex(ret, msg):
     """
     ret = abs(ret)
     if ret in errno_to_exception:
-        return errno_to_exception[ret](msg)
+        return errno_to_exception[ret](ret, msg)
     else:
         return Error(msg + (": error code %d" % ret))