-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handler.py
More file actions
53 lines (44 loc) · 2.14 KB
/
error_handler.py
File metadata and controls
53 lines (44 loc) · 2.14 KB
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
import os,sys,inspect
def get_file_location():
try:
frame = inspect.stack()[7]
module = inspect.getmodule(frame[0])
filename = module.__file__
return filename
except:
return "????"
def check_if_valid_path(path:str, class_name = None):
"""checks if a path exsists in the system, returns an unaltered path if True, exits if false
Args:
path (string): the path of the file that should exsist
method_name (string): method names should be given as inspect.stack()[0][3] or a string.
Returns:
str: returns a unalterd path
"""
if not os.path.exists(path):
print("----------------------BEGIN_ERROR_MESSAGE----------------------")
print(f"The path:{path} dosen't seem to exsist")
print(f"FILE: The error occurred in {get_file_location()}")
if not class_name == None:
print(f"CLASS: The class where the error occurred is {class_name}")
print(f"METHOD: The method the errors occurred in is {inspect.stack()[1][3]}")
print("----------------------END_ERROR_MESSAGE----------------------")
sys.exit()
return path
def custom_error_check(error_method:bool, msg:str, class_name = None, exit_program=True):
"""checks if some error method returns true the message will be printet torgeter with some aditional information
Args:
error_method (bool): method that check if an error occurred
msg (str): message that should be printet
method_name (str, optional): method names should be given as inspect.stack()[0][3] or a string. Defaults to None.
"""
if not error_method:
print("----------------------BEGIN_ERROR_MESSAGE----------------------")
print(msg)
print(f"FILE: The error occurred in {get_file_location()}")
if not class_name == None:
print(f"CLASS: The class where the error occurred is {class_name}")
print(f"METHOD: The method the errors occurred in is {inspect.stack()[1][3]}")
print("----------------------END_ERROR_MESSAGE----------------------")
if exit_program:
sys.exit()