mrradix commited on
Commit
c087535
·
verified ·
1 Parent(s): a6f0289

Update utils/error_handling.py

Browse files
Files changed (1) hide show
  1. utils/error_handling.py +83 -0
utils/error_handling.py CHANGED
@@ -12,7 +12,90 @@ from utils.logging import get_logger, log_error
12
 
13
  # Initialize logger
14
  logger = get_logger(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  # Custom exceptions
18
  class MonaError(Exception):
 
12
 
13
  # Initialize logger
14
  logger = get_logger(__name__)
15
+ # utils/error_handling.py
16
+ import streamlit as st
17
+ import traceback
18
+ from typing import Any, Callable
19
+ import functools
20
+
21
+ # Import your logging utilities
22
+ from utils.logging import get_logger
23
+
24
+ # Custom exception classes
25
+ class DataError(Exception):
26
+ """Custom exception for data-related errors"""
27
+ pass
28
+
29
+ class ValidationError(Exception):
30
+ """Custom exception for validation errors"""
31
+ pass
32
+
33
+ # Get logger without passing __name__ if get_logger() doesn't accept arguments
34
+ logger = get_logger() # Remove __name__ argument
35
+
36
+ def handle_data_exceptions(func: Callable) -> Callable:
37
+ """
38
+ Decorator to handle data-related exceptions gracefully
39
+
40
+ Args:
41
+ func: Function to wrap with exception handling
42
+
43
+ Returns:
44
+ Wrapped function with exception handling
45
+ """
46
+ @functools.wraps(func)
47
+ def wrapper(*args, **kwargs):
48
+ try:
49
+ return func(*args, **kwargs)
50
+ except (DataError, ValidationError) as e:
51
+ logger.error(f"Data handling error in {func.__name__}: {str(e)}")
52
+ st.error(f"Data Error: {str(e)}")
53
+ return None
54
+ except Exception as e:
55
+ logger.error(f"Unexpected error in {func.__name__}: {str(e)}")
56
+ logger.error(f"Traceback: {traceback.format_exc()}")
57
+ st.error(f"An unexpected error occurred: {str(e)}")
58
+ return None
59
+
60
+ return wrapper
61
 
62
+ def validate_data(data: Any, data_type: str) -> bool:
63
+ """
64
+ Validate data based on expected type
65
+
66
+ Args:
67
+ data: Data to validate
68
+ data_type: Expected data type
69
+
70
+ Returns:
71
+ bool: True if valid, False otherwise
72
+
73
+ Raises:
74
+ ValidationError: If validation fails
75
+ """
76
+ if data is None:
77
+ raise ValidationError(f"Data cannot be None for type: {data_type}")
78
+
79
+ if data_type == "dataframe":
80
+ import pandas as pd
81
+ if not isinstance(data, pd.DataFrame):
82
+ raise ValidationError("Expected pandas DataFrame")
83
+ if data.empty:
84
+ raise ValidationError("DataFrame cannot be empty")
85
+
86
+ elif data_type == "list":
87
+ if not isinstance(data, list):
88
+ raise ValidationError("Expected list")
89
+ if len(data) == 0:
90
+ raise ValidationError("List cannot be empty")
91
+
92
+ elif data_type == "dict":
93
+ if not isinstance(data, dict):
94
+ raise ValidationError("Expected dictionary")
95
+ if len(data) == 0:
96
+ raise ValidationError("Dictionary cannot be empty")
97
+
98
+ return True
99
 
100
  # Custom exceptions
101
  class MonaError(Exception):