-from typing import Any, Callable, Tuple
+from typing import Any, Callable, Iterator, Tuple
+import contextlib
import errno
import functools
import object_format
from mgr_module import CLICommand
+from . import resourcelib
from .proto import Self
sort_yaml=False,
)
rsp = object_format.Responder(_fmt)
- self._command = cc(rsp(func))
+ ewrap = error_wrapper()
+ self._command = cc(rsp(ewrap(func)))
return self
def __get__(self, obj: Any, objtype: Any = None) -> _cmdlet:
class InvalidInputValue(object_format.ErrorResponseBase):
def format_response(self) -> Tuple[int, str, str]:
return -errno.EINVAL, "", str(self)
+
+
+@contextlib.contextmanager
+def error_wrapper() -> Iterator[None]:
+ """Context-decorator that converts between certain common exception types."""
+ try:
+ yield
+ except resourcelib.ResourceTypeError as err:
+ msg = f'failed to parse input: {err}'
+ raise InvalidInputValue(msg) from err