MCPcopy Create free account
hub / github.com/apple/axlearn / _wrap_exception

Function _wrap_exception

axlearn/common/file_system.py:24–48  ·  view source on GitHub ↗

Surfaces any `source_exc` as `target_exc` instead. Users can use multiple contexts to wrapping different `target_exc`, e.g: ``` with ( _wrap_exception(tf_errors.OpError, OpError), _wrap_exception(tf_errors.NotFound, NotFoundError), ): ... ``` Note th

(
    source_exc: Union[type[Exception], tuple[type[Exception], ...]], target_exc: type[Exception]
)

Source from the content-addressed store, hash-verified

22
23@contextlib.contextmanager
24def _wrap_exception(
25 source_exc: Union[type[Exception], tuple[type[Exception], ...]], target_exc: type[Exception]
26):
27 """Surfaces any `source_exc` as `target_exc` instead.
28
29 Users can use multiple contexts to wrapping different `target_exc`, e.g:
30 ```
31 with (
32 _wrap_exception(tf_errors.OpError, OpError),
33 _wrap_exception(tf_errors.NotFound, NotFoundError),
34 ):
35 ...
36 ```
37
38 Note that contexts are entered in order and exited in reverse order, so more general exceptions
39 should be wrapped first.
40
41 Args:
42 source_exc: One or more exceptions to catch.
43 target_exc: Exception to raise instead.
44 """
45 try:
46 yield
47 except source_exc as e:
48 raise target_exc(str(e)) from e
49
50
51class OpError(Exception):

Callers 2

wrappedFunction · 0.85
globFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected