| 1 |
# Copyright (c) 2024 Ansible Project |
| 2 |
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 3 |
|
| 4 |
"""Internal error handling logic for targets. Not for use on the controller.""" |
| 5 |
|
| 6 |
from __future__ import annotations as _annotations |
| 7 |
|
| 8 |
import traceback as _sys_traceback |
| 9 |
import typing as _t |
| 10 |
|
| 11 |
from . import _messages |
| 12 |
|
| 13 |
MSG_REASON_DIRECT_CAUSE: _t.Final[str] = '<<< caused by >>>' |
| 14 |
MSG_REASON_HANDLING_CAUSE: _t.Final[str] = '<<< while handling >>>' |
| 15 |
|
| 16 |
TRACEBACK_REASON_EXCEPTION_DIRECT_WARNING: _t.Final[str] = 'The above exception was the direct cause of the following warning:' |
| 17 |
|
| 18 |
|
| 19 |
class EventFactory: |
| 20 |
"""Factory for creating `Event` instances from `BaseException` instances on targets.""" |
| 21 |
|
| 22 |
_MAX_DEPTH = 10 |
| 23 |
"""Maximum exception chain depth. Exceptions beyond this depth will be omitted.""" |
| 24 |
|
| 25 |
@classmethod |
| 26 |
def from_exception(cls, exception: BaseException, include_traceback: bool) -> _messages.Event: |
| 27 |
return cls(include_traceback)._convert_exception(exception) |
| 28 |
|
| 29 |
def __init__(self, include_traceback: bool) -> None: |
| 30 |
self._include_traceback = include_traceback |
| 31 |
self._depth = 0 |
| 32 |
|
| 33 |
def _convert_exception(self, exception: BaseException) -> _messages.Event: |
| 34 |
if self._depth > self._MAX_DEPTH: |
| 35 |
return _messages.Event( |
| 36 |
msg="Maximum depth exceeded, omitting further events.", |
| 37 |
) |
| 38 |
|
| 39 |
self._depth += 1 |
| 40 |
|
| 41 |
try: |
| 42 |
return _messages.Event( |
| 43 |
msg=self._get_msg(exception), |
| 44 |
formatted_traceback=self._get_formatted_traceback(exception), |
| 45 |
formatted_source_context=self._get_formatted_source_context(exception), |
| 46 |
help_text=self._get_help_text(exception), |
| 47 |
chain=self._get_chain(exception), |
| 48 |
events=self._get_events(exception), |
| 49 |
) |
| 50 |
finally: |
| 51 |
self._depth -= 1 |
| 52 |
|
| 53 |
def _get_msg(self, exception: BaseException) -> str | None: |
| 54 |
return str(exception).strip() |
| 55 |
|
| 56 |
def _get_formatted_traceback(self, exception: BaseException) -> str | None: |
| 57 |
if self._include_traceback: |
| 58 |
return ''.join(_sys_traceback.format_exception(type(exception), exception, exception.__traceback__, chain=False)) |
| 59 |
|
| 60 |
return None |
| 61 |
|
| 62 |
def _get_formatted_source_context(self, exception: BaseException) -> str | None: |
| 63 |
return None |
| 64 |
|
| 65 |
def _get_help_text(self, exception: BaseException) -> str | None: |
| 66 |
return None |
| 67 |
|
| 68 |
def _get_chain(self, exception: BaseException) -> _messages.EventChain | None: |
| 69 |
if cause := self._get_cause(exception): |
| 70 |
return _messages.EventChain( |
| 71 |
msg_reason=MSG_REASON_DIRECT_CAUSE, |
| 72 |
traceback_reason='The above exception was the direct cause of the following exception:', |
| 73 |
event=self._convert_exception(cause), |
| 74 |
follow=self._follow_cause(exception), |
| 75 |
) |
| 76 |
|
| 77 |
if context := self._get_context(exception): |
| 78 |
return _messages.EventChain( |
| 79 |
msg_reason=MSG_REASON_HANDLING_CAUSE, |
| 80 |
traceback_reason='During handling of the above exception, another exception occurred:', |
| 81 |
event=self._convert_exception(context), |
| 82 |
follow=False, |
| 83 |
) |
| 84 |
|
| 85 |
return None |
| 86 |
|
| 87 |
def _follow_cause(self, exception: BaseException) -> bool: |
| 88 |
return True |
| 89 |
|
| 90 |
def _get_cause(self, exception: BaseException) -> BaseException | None: |
| 91 |
return exception.__cause__ |
| 92 |
|
| 93 |
def _get_context(self, exception: BaseException) -> BaseException | None: |
| 94 |
if exception.__suppress_context__: |
| 95 |
return None |
| 96 |
|
| 97 |
return exception.__context__ |
| 98 |
|
| 99 |
def _get_events(self, exception: BaseException) -> tuple[_messages.Event, ...] | None: |
| 100 |
# deprecated: description='move BaseExceptionGroup support here from ControllerEventFactory' python_version='3.10' |
| 101 |
return None |