Skip to content

Commit d789764

Browse files
mgr/smb: fix error handling for fundamental resource parsing
When an smb resource is input to the smb mgr module in YAML or JSON the fundamental parsing/deserialization is handled by resourcelib. This module tries to be largely independent of smb mgr module and defines a few basic exception types. When these exception types were raised the `ceph` command line would print out a long traceback. Avoid printing a traceback by catching these errors with a new contextmanager (decorator) that is automatically called when using the smb ceph mgr command api. Fixes: https://tracker.ceph.com/issues/71992 Signed-off-by: John Mulligan <[email protected]>
1 parent d1afa98 commit d789764

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/pybind/mgr/smb/cli.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from typing import Any, Callable, Tuple
1+
from typing import Any, Callable, Iterator, Tuple
22

3+
import contextlib
34
import errno
45
import functools
56

67
import object_format
78
from mgr_module import CLICommand
89

10+
from . import resourcelib
911
from .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:
6669
class 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

Comments
 (0)