Возвращает первое исключение заданного типа из цепочки, или None.
(exc: Exception, target_type: type, max_depth: int = 10)
| 12 | # ── Утилиты для обхода цепочки исключений ──────────────────────────────────── |
| 13 | |
| 14 | def find_cause(exc: Exception, target_type: type, max_depth: int = 10) -> Optional[Exception]: |
| 15 | """Возвращает первое исключение заданного типа из цепочки, или None.""" |
| 16 | current = exc |
| 17 | for _ in range(max_depth): |
| 18 | if isinstance(current, target_type): |
| 19 | return current |
| 20 | nxt = current.__cause__ or current.__context__ |
| 21 | if nxt is None: |
| 22 | break |
| 23 | current = nxt |
| 24 | return None |
| 25 | |
| 26 | |
| 27 | def get_errno_from_chain(exc: Exception, max_depth: int = 10) -> Optional[int]: |
no outgoing calls
no test coverage detected