-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomErrors.py
60 lines (51 loc) · 2 KB
/
CustomErrors.py
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
50
51
52
53
54
55
56
57
58
59
60
"""
This class contains custom errors that are used frequently throughout the project.
"""
from typing import Any, List, Union
class RecursiveTypeError(TypeError):
"""
This class treats a TypeError raised during recursion or recursion-esque methods.
"""
def __init__(self, err_var: Any, safe_cl: Union[type, List[type]] = str, err_name: str = "input") -> None:
"""
Constructor for :class:`~CustomErrors.RecursiveTypeError`
:param err_var: variable that caused error to be raised
:param safe_cl: accepted classes that won't cause error to be raised
:param err_name: original name of error-causing variable
"""
self.variable = err_var
self.name = err_name
self.safe_classes = [safe_cl] if not isinstance(safe_cl, list) else safe_cl
super().__init__(self.getMessage())
def getVariable(self) -> Any:
"""
Get variable that caused error to be raised.
"""
return self.variable
def getVariableClass(self) -> str:
"""
Get class of error-causing variable.
"""
return self.getVariable().__class__.__name__
def getVariableName(self) -> str:
"""
Get original name of error-causing variable.
"""
return self.name
def getSafeClasses(self) -> str:
"""
Get classes that prevent error from being raised.
"""
cl_strs = [cl.__name__ for cl in self.safe_classes]
cl_str = ', '.join(cl_strs)
return cl_str
def getMessage(self) -> str:
"""
Get message with including (1) info about error-causing variable and (2) error-preventing classes.
"""
var = self.getVariable()
cl = self.getVariableClass()
name = self.getVariableName()
safe_cls = self.getSafeClasses()
message = f"{name:s} ({cl:s}, {var:}) must be of type {safe_cls:s} or list thereof"
return message