File: os_error.py

package info (click to toggle)
python-friendly-traceback 0.7.62%2Bgit20240811.d7dbff6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,264 kB
  • sloc: python: 21,500; makefile: 4
file content (49 lines) | stat: -rw-r--r-- 1,865 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from ..ft_gettext import current_lang
from ..message_parser import get_parser
from ..tb_data import TracebackData  # for type checking only
from ..typing_info import CauseInfo  # for type checking only

parser = get_parser(OSError)
_ = current_lang.translate


@parser._add
def handle_connection_error(_message: OSError, tb_data: TracebackData) -> CauseInfo:
    tb = "\n".join(tb_data.formatted_tb)
    if (
        "socket.gaierror" in tb
        or "urllib.error" in tb
        or "urllib3.exception" in tb
        or "requests.exception" in tb
    ):
        cause = _(
            "I suspect that you are trying to connect to a server and\n"
            "that a connection cannot be made.\n\n"
            "If that is the case, check for typos in the URL\n"
            "and check your internet connectivity.\n"
        )
        return {"cause": cause}
    return {}


@parser._add
def invalid_argument(message: str, tb_data: TracebackData) -> CauseInfo:
    if "Invalid argument:" not in message:
        return {}
    value = tb_data.value
    filename = value.filename
    repr_filename = repr(filename)
    if "\\" not in filename and "\\" in repr_filename:
        hint = _("Perhaps you need to double the backslash characters.\n")
        cause = _(
            "I suspect that you wrote a filename or path that contains\n"
            "at least one backslash character, `\\`.\n"
            "Python likely interpreted this as indicating the beginning of\n"
            "what is known as an escape sequence.\n"
            "To solve the problem, either write a so-called 'raw string'\n"
            "by adding the letter `r` as a prefix in\n"
            "front of the filename or path, or replace all single backslash\n"
            "characters, `\\`, by double ones: `\\\\`.\n"
        )
        return {"cause": cause, "suggest": hint}
    return {}