Debian Codegraph @

friendly_traceback/runtime_errors/os_error.py

raw symbols SCIP
1 from ..ft_gettext import current_lang
2 from ..message_parser import get_parser
3 from ..tb_data import TracebackData # for type checking only
4 from ..typing_info import CauseInfo # for type checking only
5
6 parser = get_parser(OSError)
7 _ = current_lang.translate
8
9
10 @parser._add
11 def handle_connection_error(_message: OSError, tb_data: TracebackData) -> CauseInfo:
12 tb = "\n".join(tb_data.formatted_tb)
13 if (
14 "socket.gaierror" in tb
15 or "urllib.error" in tb
16 or "urllib3.exception" in tb
17 or "requests.exception" in tb
18 ):
19 cause = _(
20 "I suspect that you are trying to connect to a server and\n"
21 "that a connection cannot be made.\n\n"
22 "If that is the case, check for typos in the URL\n"
23 "and check your internet connectivity.\n"
24 )
25 return {"cause": cause}
26 return {}
27
28
29 @parser._add
30 def invalid_argument(message: str, tb_data: TracebackData) -> CauseInfo:
31 if "Invalid argument:" not in message:
32 return {}
33 value = tb_data.value
34 filename = value.filename
35 repr_filename = repr(filename)
36 if "\\" not in filename and "\\" in repr_filename:
37 hint = _("Perhaps you need to double the backslash characters.\n")
38 cause = _(
39 "I suspect that you wrote a filename or path that contains\n"
40 "at least one backslash character, `\\`.\n"
41 "Python likely interpreted this as indicating the beginning of\n"
42 "what is known as an escape sequence.\n"
43 "To solve the problem, either write a so-called 'raw string'\n"
44 "by adding the letter `r` as a prefix in\n"
45 "front of the filename or path, or replace all single backslash\n"
46 "characters, `\\`, by double ones: `\\\\`.\n"
47 )
48 return {"cause": cause, "suggest": hint}
49 return {}