(
exc: BaseException, include_subexception_msg: bool = True
)
| 465 | |
| 466 | |
| 467 | def stringify_exception( |
| 468 | exc: BaseException, include_subexception_msg: bool = True |
| 469 | ) -> str: |
| 470 | try: |
| 471 | notes = getattr(exc, "__notes__", []) |
| 472 | except KeyError: |
| 473 | # Workaround for https://github.com/python/cpython/issues/98778 on |
| 474 | # some 3.10 and 3.11 patch versions. |
| 475 | HTTPError = getattr(sys.modules.get("urllib.error", None), "HTTPError", ()) |
| 476 | if sys.version_info < (3, 12) and isinstance(exc, HTTPError): |
| 477 | notes = [] |
| 478 | else: # pragma: no cover |
| 479 | # exception not related to above bug, reraise |
| 480 | raise |
| 481 | if not include_subexception_msg and isinstance(exc, BaseExceptionGroup): |
| 482 | message = exc.message |
| 483 | else: |
| 484 | message = str(exc) |
| 485 | |
| 486 | return "\n".join( |
| 487 | [ |
| 488 | message, |
| 489 | *notes, |
| 490 | ] |
| 491 | ) |
| 492 | |
| 493 | |
| 494 | E = TypeVar("E", bound=BaseException, covariant=True) |
no test coverage detected