| 1 |
"""This module contains the necessary class and functions needed to |
| 2 |
help finding the cause (specific information aka answer to ``why()``) |
| 3 |
of an exception. Most of the content should be considered to be private. |
| 4 |
|
| 5 |
It does contain one function (``get_parser``) |
| 6 |
which is intended to be part of the public API, but needs to be |
| 7 |
imported from this module instead of simply from ``friendly_traceback``. |
| 8 |
""" |
| 9 |
|
| 10 |
from importlib import import_module |
| 11 |
from typing import List, Type, TypeVar |
| 12 |
|
| 13 |
from . import debug_helper |
| 14 |
from .ft_gettext import internal_error, no_information, unknown_case |
| 15 |
from .tb_data import TracebackData |
| 16 |
from .typing_info import _E, CauseInfo, Parser |
| 17 |
|
| 18 |
_P = TypeVar("_P", bound=Parser) |
| 19 |
|
| 20 |
INCLUDED_PARSERS = { |
| 21 |
AttributeError: "attribute_error", |
| 22 |
FileNotFoundError: "file_not_found_error", |
| 23 |
ImportError: "import_error", |
| 24 |
IndexError: "index_error", |
| 25 |
KeyError: "key_error", |
| 26 |
ModuleNotFoundError: "module_not_found_error", |
| 27 |
NameError: "name_error", |
| 28 |
OSError: "os_error", |
| 29 |
RuntimeError: "runtime_error", |
| 30 |
TypeError: "type_error", |
| 31 |
UnboundLocalError: "unbound_local_error", |
| 32 |
ValueError: "value_error", |
| 33 |
ZeroDivisionError: "zero_division_error", |
| 34 |
} |
| 35 |
RUNTIME_MESSAGE_PARSERS = {} |
| 36 |
|
| 37 |
|
| 38 |
class RuntimeMessageParser: |
| 39 |
"""This class is used to create objects that collect message parsers.""" |
| 40 |
|
| 41 |
def __init__(self) -> None: |
| 42 |
self.parsers: List[Parser] = [] |
| 43 |
self.core_parsers: List[Parser] = [] |
| 44 |
self.custom_parsers: List[Parser] = [] |
| 45 |
|
| 46 |
def _add(self, func: _P) -> _P: |
| 47 |
"""This method is meant to be used only within friendly-traceback. |
| 48 |
It is used as a decorator to add a message parser to a list that is |
| 49 |
automatically updated. |
| 50 |
""" |
| 51 |
self.parsers.append(func) |
| 52 |
self.core_parsers.append(func) |
| 53 |
return func |
| 54 |
|
| 55 |
def add(self, func: _P) -> _P: |
| 56 |
"""This method is meant to be used by projects that extend |
| 57 |
friendly-traceback. It is used as a decorator to add a message parser |
| 58 |
to a list that is automatically updated:: |
| 59 |
|
| 60 |
@instance.add |
| 61 |
def some_message_parser(message, traceback_data): |
| 62 |
.... |
| 63 |
""" |
| 64 |
self.custom_parsers.append(func) |
| 65 |
self.parsers = self.custom_parsers + self.core_parsers |
| 66 |
return func |
| 67 |
|
| 68 |
|
| 69 |
def get_parser(exception_type: Type[_E]) -> RuntimeMessageParser: |
| 70 |
"""Gets a 'parser' to find the cause for a given exception. |
| 71 |
|
| 72 |
Args: |
| 73 |
exception_type: an exception class. |
| 74 |
|
| 75 |
Usage:: |
| 76 |
|
| 77 |
parser = get_parser(SomeSpecificError) |
| 78 |
|
| 79 |
@parser.add |
| 80 |
def some_meaningful_name(message: str, |
| 81 |
tb_data: TracebackData) -> dict: |
| 82 |
if not handled_by_this_function(message): |
| 83 |
return {} # let other parsers deal with it |
| 84 |
|
| 85 |
... |
| 86 |
""" |
| 87 |
if exception_type not in RUNTIME_MESSAGE_PARSERS: |
| 88 |
RUNTIME_MESSAGE_PARSERS[exception_type] = RuntimeMessageParser() |
| 89 |
if exception_type in INCLUDED_PARSERS: |
| 90 |
base_path = "friendly_traceback.runtime_errors." |
| 91 |
import_module(base_path + INCLUDED_PARSERS[exception_type]) |
| 92 |
return RUNTIME_MESSAGE_PARSERS[exception_type] |
| 93 |
|
| 94 |
|
| 95 |
def get_likely_cause( |
| 96 |
exception_type, |
| 97 |
message: str, |
| 98 |
tb_data: TracebackData, |
| 99 |
) -> CauseInfo: |
| 100 |
"""Attempts to get the likely cause of an exception.""" |
| 101 |
try: |
| 102 |
return get_cause(exception_type, message, tb_data) |
| 103 |
except Exception as e: |
| 104 |
debug_helper.log(message) |
| 105 |
return {"cause": internal_error(e), "suggest": internal_error(e)} |
| 106 |
|
| 107 |
|
| 108 |
def get_cause( |
| 109 |
exception_type, |
| 110 |
message: str, |
| 111 |
tb_data: TracebackData, |
| 112 |
) -> CauseInfo: |
| 113 |
"""For a given exception type, cycle through the known message parsers, |
| 114 |
looking for one that can find a cause of the exception.""" |
| 115 |
message_parser = get_parser(exception_type) |
| 116 |
|
| 117 |
for parser in message_parser.parsers: |
| 118 |
|
| 119 |
cause = parser(message, tb_data) |
| 120 |
if cause: |
| 121 |
return cause |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
try: |
| 126 |
if issubclass(exception_type, OSError): |
| 127 |
os_error_parser = get_parser(OSError) |
| 128 |
for parser in os_error_parser.parsers: |
| 129 |
cause = parser(message, tb_data) |
| 130 |
if cause: |
| 131 |
return cause |
| 132 |
else: |
| 133 |
return {"cause": no_information(), "suggest": unknown_case()} |
| 134 |
except Exception: |
| 135 |
pass |
| 136 |
|
| 137 |
if not message_parser.parsers: |
| 138 |
return {} |
| 139 |
|
| 140 |
debug_helper.log(str(message)) |
| 141 |
return {"cause": no_information(), "suggest": unknown_case()} |