Format ``exc`` as ``"ClassName: message"`` for substring/regex matching. Uses `traceback.format_exception_only` so the class name is preserved and callers can match on either the class name or the message. Args: exc: The exception to format. include_cause: If True and `
(exc: BaseException, include_cause: bool = False)
| 19 | |
| 20 | |
| 21 | def format_exception(exc: BaseException, include_cause: bool = False) -> str: |
| 22 | """Format ``exc`` as ``"ClassName: message"`` for substring/regex matching. |
| 23 | |
| 24 | Uses `traceback.format_exception_only` so the class name is preserved |
| 25 | and callers can match on either the class name or the message. |
| 26 | |
| 27 | Args: |
| 28 | exc: The exception to format. |
| 29 | include_cause: If True and ``exc.__cause__`` is set (``raise X from Y``), |
| 30 | append the cause exception after the base exception. This is useful when |
| 31 | we want to match on an exception encountered in the UDF (e.g. ``RateLimitError``) |
| 32 | which is wrapped in a ``UserCodeException`` by Ray Data. |
| 33 | |
| 34 | Returns: |
| 35 | A single-string representation of ``exc`` in the form |
| 36 | ``"ClassName: message"``. When ``include_cause`` is True and |
| 37 | ``exc.__cause__`` is set, the cause's formatted form is appended |
| 38 | after a single space. See the example below. |
| 39 | |
| 40 | Example: |
| 41 | For a ``UserCodeException`` wrapping a ``RateLimitError``, calling ``format_exception(e, include_cause=True)`` |
| 42 | returns:: |
| 43 | |
| 44 | ray.exceptions.UserCodeException: UDF failed to process a data block. RateLimitError: Error code: 429 - rate limited |
| 45 | """ |
| 46 | s = "".join(traceback.format_exception_only(type(exc), exc)).rstrip("\n") |
| 47 | if include_cause and exc.__cause__: |
| 48 | cause = exc.__cause__ |
| 49 | s += " " + "".join(traceback.format_exception_only(type(cause), cause)).rstrip( |
| 50 | "\n" |
| 51 | ) |
| 52 | return s |
| 53 | |
| 54 | |
| 55 | def matches_error(pattern: str, error_str: str) -> bool: |
no test coverage detected
searching dependent graphs…