Update utils/error_handling.py
Browse files- utils/error_handling.py +52 -80
utils/error_handling.py
CHANGED
@@ -1,84 +1,56 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
|
4 |
-
from
|
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 |
-
def handle_ui_exceptions(func):
|
35 |
-
"""Decorator to handle UI-related exceptions"""
|
36 |
-
@functools.wraps(func)
|
37 |
-
def wrapper(*args, **kwargs):
|
38 |
-
try:
|
39 |
-
return func(*args, **kwargs)
|
40 |
-
except Exception as e:
|
41 |
-
log_error(f"UI error in {func.__name__}", error=e)
|
42 |
-
st.error(f"Interface error: {str(e)}")
|
43 |
-
return None
|
44 |
-
return wrapper
|
45 |
-
|
46 |
-
def safe_execute(func, *args, **kwargs):
|
47 |
-
"""Safely execute a function with error handling"""
|
48 |
-
try:
|
49 |
-
return func(*args, **kwargs)
|
50 |
-
except Exception as e:
|
51 |
-
log_error(f"Error executing {func.__name__}", error=e)
|
52 |
-
return None
|
53 |
|
54 |
-
def
|
55 |
-
"""
|
56 |
-
|
57 |
-
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
|
66 |
-
def
|
67 |
-
"""
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
log_error(f"File not found in {func.__name__}", error=e)
|
74 |
-
st.error("File not found. Please check the file path.")
|
75 |
-
return None
|
76 |
-
except PermissionError as e:
|
77 |
-
log_error(f"Permission error in {func.__name__}", error=e)
|
78 |
-
st.error("Permission denied. Please check file permissions.")
|
79 |
-
return None
|
80 |
-
except Exception as e:
|
81 |
-
log_error(f"File operation error in {func.__name__}", error=e)
|
82 |
-
st.error(f"File operation failed: {str(e)}")
|
83 |
-
return None
|
84 |
-
return wrapper
|
|
|
1 |
+
import logging
|
2 |
+
import sys
|
3 |
+
from datetime import datetime
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
# Global logger configuration
|
7 |
+
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
8 |
+
LOG_LEVEL = logging.INFO
|
9 |
+
|
10 |
+
def setup_logging(log_level=LOG_LEVEL):
|
11 |
+
"""Setup logging configuration"""
|
12 |
+
logging.basicConfig(
|
13 |
+
level=log_level,
|
14 |
+
format=LOG_FORMAT,
|
15 |
+
handlers=[
|
16 |
+
logging.StreamHandler(sys.stdout),
|
17 |
+
]
|
18 |
+
)
|
19 |
+
|
20 |
+
def get_logger(name):
|
21 |
+
"""Get a logger instance"""
|
22 |
+
return logging.getLogger(name)
|
23 |
+
|
24 |
+
def log_error(logger, error_msg, exception=None, context=None):
|
25 |
+
"""Log error with additional context"""
|
26 |
+
full_msg = f"{error_msg}"
|
27 |
+
if context:
|
28 |
+
full_msg += f" | Context: {context}"
|
29 |
+
if exception:
|
30 |
+
full_msg += f" | Exception: {str(exception)}"
|
31 |
+
|
32 |
+
logger.error(full_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
def log_info(logger, msg, context=None):
|
35 |
+
"""Log info with additional context"""
|
36 |
+
full_msg = f"{msg}"
|
37 |
+
if context:
|
38 |
+
full_msg += f" | Context: {context}"
|
39 |
|
40 |
+
logger.info(full_msg)
|
41 |
+
|
42 |
+
def log_warning(logger, msg, context=None):
|
43 |
+
"""Log warning with additional context"""
|
44 |
+
full_msg = f"{msg}"
|
45 |
+
if context:
|
46 |
+
full_msg += f" | Context: {context}"
|
47 |
|
48 |
+
logger.warning(full_msg)
|
49 |
|
50 |
+
def log_debug(logger, msg, context=None):
|
51 |
+
"""Log debug with additional context"""
|
52 |
+
full_msg = f"{msg}"
|
53 |
+
if context:
|
54 |
+
full_msg += f" | Context: {context}"
|
55 |
+
|
56 |
+
logger.debug(full_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|