mrradix commited on
Commit
d249ccb
·
verified ·
1 Parent(s): 53ccced

Update utils/error_handling.py

Browse files
Files changed (1) hide show
  1. utils/error_handling.py +52 -80
utils/error_handling.py CHANGED
@@ -1,84 +1,56 @@
1
- import functools
2
- import traceback
3
- import streamlit as st
4
- from utils.logging import log_error, get_logger
5
-
6
- class DataError(Exception):
7
- """Custom exception for data-related errors"""
8
- pass
9
-
10
- class ValidationError(Exception):
11
- """Custom exception for validation errors"""
12
- pass
13
-
14
- class ConfigError(Exception):
15
- """Custom exception for configuration errors"""
16
- pass
17
-
18
- def handle_data_exceptions(func):
19
- """Decorator to handle data-related exceptions"""
20
- @functools.wraps(func)
21
- def wrapper(*args, **kwargs):
22
- try:
23
- return func(*args, **kwargs)
24
- except (DataError, ValidationError) as e:
25
- log_error(f"Data error in {func.__name__}", error=e)
26
- st.error(f"Data processing error: {str(e)}")
27
- return None
28
- except Exception as e:
29
- log_error(f"Unexpected error in {func.__name__}", error=e)
30
- st.error(f"An unexpected error occurred: {str(e)}")
31
- return None
32
- return wrapper
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 validate_input(data, required_fields=None):
55
- """Validate input data"""
56
- if not data:
57
- raise ValidationError("Input data is empty or None")
 
58
 
59
- if required_fields:
60
- missing_fields = [field for field in required_fields if field not in data]
61
- if missing_fields:
62
- raise ValidationError(f"Missing required fields: {', '.join(missing_fields)}")
 
 
 
63
 
64
- return True
65
 
66
- def handle_file_operations(func):
67
- """Decorator to handle file operation exceptions"""
68
- @functools.wraps(func)
69
- def wrapper(*args, **kwargs):
70
- try:
71
- return func(*args, **kwargs)
72
- except FileNotFoundError as e:
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)