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]
)
| 22 | |
| 23 | @contextlib.contextmanager |
| 24 | def _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 | |
| 51 | class OpError(Exception): |