]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/volumes: use `volume_exception_to_retval` as a decorator
authorLeonid Usov <leonid.usov@ibm.com>
Thu, 4 Jan 2024 17:51:32 +0000 (19:51 +0200)
committerLeonid Usov <leonid.usov@ibm.com>
Mon, 4 Mar 2024 11:48:03 +0000 (13:48 +0200)
When used as a decorator, it saves one indented try-catch block inside the decorated method.
This can be applied to most of the methods in the file, subject to a separate refactoring commit

Signed-off-by: Leonid Usov <leonid.usov@ibm.com>
src/pybind/mgr/volumes/fs/volume.py

index 0c4a075980540956b2c5376468a3cdf67f29ac50..8e89fed007a122d3194a59e660c93d1eeefcbf7c 100644 (file)
@@ -2,7 +2,9 @@ import json
 import errno
 import logging
 import mgr_util
-from typing import TYPE_CHECKING
+import inspect
+import functools
+from typing import TYPE_CHECKING, Any, Callable, Optional
 
 import cephfs
 
@@ -86,11 +88,28 @@ class VolumeClient(CephfsClient["Module"]):
             lvl = self.mgr.ClusterLogPrio.WARN
         self.mgr.cluster_log("cluster", lvl, msg)
 
-    def volume_exception_to_retval(self, ve):
+    def volume_exception_to_retval(self_or_method: Any, ve: Optional[VolumeException] = None):
         """
         return a tuple representation from a volume exception
+        OR wrap the decorated method into a try:catch:
+        that will convert VolumeException to the tuple
         """
-        return ve.to_tuple()
+        if ve is None and callable(self_or_method):
+            # used as a decorator
+            method: Callable = self_or_method
+            @functools.wraps(method)
+            def wrapper(self, *args, **kwargs):
+                try:
+                    return method(self, *args, **kwargs)
+                except VolumeException as ve:
+                    return self.volume_exception_to_retval(ve)
+            return wrapper
+        elif ve is not None:
+            # used as a method on self with a VolumeException argument
+            return ve.to_tuple()
+        else:
+            # shouldn't get here, bad call
+            assert(ve is not None)
 
     ### volume operations -- create, rm, ls