1- from typing import Any , Callable , Tuple
1+ from typing import Any , Callable , Iterator , Tuple
22
3+ import contextlib
34import errno
45import functools
56
67import object_format
78from mgr_module import CLICommand
89
10+ from . import resourcelib
911from .proto import Self
1012
1113
@@ -53,7 +55,8 @@ def __call__(self, func: Callable) -> Self:
5355 sort_yaml = False ,
5456 )
5557 rsp = object_format .Responder (_fmt )
56- self ._command = cc (rsp (func ))
58+ ewrap = error_wrapper ()
59+ self ._command = cc (rsp (ewrap (func )))
5760 return self
5861
5962 def __get__ (self , obj : Any , objtype : Any = None ) -> _cmdlet :
@@ -66,3 +69,13 @@ def __get__(self, obj: Any, objtype: Any = None) -> _cmdlet:
6669class InvalidInputValue (object_format .ErrorResponseBase ):
6770 def format_response (self ) -> Tuple [int , str , str ]:
6871 return - errno .EINVAL , "" , str (self )
72+
73+
74+ @contextlib .contextmanager
75+ def error_wrapper () -> Iterator [None ]:
76+ """Context-decorator that converts between certain common exception types."""
77+ try :
78+ yield
79+ except resourcelib .ResourceTypeError as err :
80+ msg = f'failed to parse input: { err } '
81+ raise InvalidInputValue (msg ) from err
0 commit comments