(e, prefix: str = '')
| 44 | |
| 45 | |
| 46 | def enhance_error(e, prefix: str = ''): |
| 47 | # Get the original exception type |
| 48 | exc_type = type(e) |
| 49 | |
| 50 | # Special handling for ExceptionGroup |
| 51 | if exc_type.__name__ == 'ExceptionGroup': |
| 52 | # Recursively enhance each sub-exception |
| 53 | new_msg = f'{prefix}: {e}' |
| 54 | new_exceptions = [enhance_error(exc, prefix) for exc in e.exceptions] |
| 55 | # Note: ExceptionGroup requires a list of exceptions |
| 56 | new_exc = BuiltInExceptionGroup(new_msg, new_exceptions) |
| 57 | return new_exc |
| 58 | |
| 59 | # For other exceptions: attempt to construct a new one, using only args[0] (the message part) |
| 60 | try: |
| 61 | # Most exception types support exc("new message") |
| 62 | new_exc = exc_type(f'{prefix}: {e}') |
| 63 | new_exc.__cause__ = e.__cause__ |
| 64 | new_exc.__context__ = e.__context__ |
| 65 | return new_exc |
| 66 | except Exception: |
| 67 | # Construction failed; fall back to a generic exception |
| 68 | return RuntimeError(f'{prefix}: {e}') |
| 69 | |
| 70 | |
| 71 | def assert_package_exist(package, message: Optional[str] = None): |
no test coverage detected