mrradix commited on
Commit
6ff8c59
·
verified ·
1 Parent(s): ce00133

Update utils/error_handling.py

Browse files
Files changed (1) hide show
  1. utils/error_handling.py +45 -53
utils/error_handling.py CHANGED
@@ -1,6 +1,6 @@
1
  import traceback
2
- from typing import Any, Callable
3
  import functools
 
4
 
5
  # Import your logging utilities
6
  from utils.logging import get_logger, log_error
@@ -14,18 +14,18 @@ class ValidationError(Exception):
14
  """Custom exception for validation errors"""
15
  pass
16
 
 
 
 
 
 
 
17
  # Get logger
18
  logger = get_logger(__name__)
19
 
20
  def handle_data_exceptions(func: Callable) -> Callable:
21
  """
22
  Decorator to handle data-related exceptions gracefully
23
-
24
- Args:
25
- func: Function to wrap with exception handling
26
-
27
- Returns:
28
- Wrapped function with exception handling
29
  """
30
  @functools.wraps(func)
31
  def wrapper(*args, **kwargs):
@@ -33,7 +33,6 @@ def handle_data_exceptions(func: Callable) -> Callable:
33
  return func(*args, **kwargs)
34
  except (DataError, ValidationError) as e:
35
  log_error(logger, f"Data handling error in {func.__name__}: {str(e)}")
36
- # Instead of streamlit error, we'll raise the exception or return None
37
  print(f"Data Error: {str(e)}")
38
  return None
39
  except Exception as e:
@@ -41,26 +40,48 @@ def handle_data_exceptions(func: Callable) -> Callable:
41
  log_error(logger, f"Traceback: {traceback.format_exc()}")
42
  print(f"An unexpected error occurred: {str(e)}")
43
  return None
44
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  return wrapper
46
 
47
  def validate_data(data: Any, data_type: str) -> bool:
48
  """
49
  Validate data based on expected type
50
-
51
- Args:
52
- data: Data to validate
53
- data_type: Expected data type
54
-
55
- Returns:
56
- bool: True if valid, False otherwise
57
-
58
- Raises:
59
- ValidationError: If validation fails
60
  """
61
  if data is None:
62
  raise ValidationError(f"Data cannot be None for type: {data_type}")
63
-
64
  if data_type == "dataframe":
65
  try:
66
  import pandas as pd
@@ -70,19 +91,19 @@ def validate_data(data: Any, data_type: str) -> bool:
70
  raise ValidationError("DataFrame cannot be empty")
71
  except ImportError:
72
  raise ValidationError("pandas not available for DataFrame validation")
73
-
74
  elif data_type == "list":
75
  if not isinstance(data, list):
76
  raise ValidationError("Expected list")
77
  if len(data) == 0:
78
  raise ValidationError("List cannot be empty")
79
-
80
  elif data_type == "dict":
81
  if not isinstance(data, dict):
82
  raise ValidationError("Expected dictionary")
83
  if len(data) == 0:
84
  raise ValidationError("Dictionary cannot be empty")
85
-
86
  return True
87
 
88
  def show_error(message: str):
@@ -115,41 +136,12 @@ def show_success(message: str):
115
  except ImportError:
116
  print(f"SUCCESS: {message}")
117
 
118
- def handle_exceptions(func: Callable) -> Callable:
119
- """
120
- General exception handler decorator
121
-
122
- Args:
123
- func: Function to wrap with exception handling
124
-
125
- Returns:
126
- Wrapped function with exception handling
127
- """
128
- @functools.wraps(func)
129
- def wrapper(*args, **kwargs):
130
- try:
131
- return func(*args, **kwargs)
132
- except Exception as e:
133
- log_error(logger, f"Exception in {func.__name__}: {str(e)}")
134
- log_error(logger, f"Traceback: {traceback.format_exc()}")
135
- return None
136
-
137
- return wrapper
138
-
139
  def safe_get(dictionary: dict, key: str, default: Any = None) -> Any:
140
  """
141
  Safely get value from dictionary
142
-
143
- Args:
144
- dictionary: Dictionary to get value from
145
- key: Key to look up
146
- default: Default value if key not found
147
-
148
- Returns:
149
- Value from dictionary or default
150
  """
151
  try:
152
  return dictionary.get(key, default)
153
  except (AttributeError, TypeError):
154
  logger.warning(f"safe_get failed for key '{key}' - dictionary is not dict-like")
155
- return default
 
1
  import traceback
 
2
  import functools
3
+ from typing import Any, Callable
4
 
5
  # Import your logging utilities
6
  from utils.logging import get_logger, log_error
 
14
  """Custom exception for validation errors"""
15
  pass
16
 
17
+ class AIModelError(Exception):
18
+ """Custom exception for AI model errors"""
19
+ def __init__(self, message, metadata=None):
20
+ super().__init__(message)
21
+ self.metadata = metadata or {}
22
+
23
  # Get logger
24
  logger = get_logger(__name__)
25
 
26
  def handle_data_exceptions(func: Callable) -> Callable:
27
  """
28
  Decorator to handle data-related exceptions gracefully
 
 
 
 
 
 
29
  """
30
  @functools.wraps(func)
31
  def wrapper(*args, **kwargs):
 
33
  return func(*args, **kwargs)
34
  except (DataError, ValidationError) as e:
35
  log_error(logger, f"Data handling error in {func.__name__}: {str(e)}")
 
36
  print(f"Data Error: {str(e)}")
37
  return None
38
  except Exception as e:
 
40
  log_error(logger, f"Traceback: {traceback.format_exc()}")
41
  print(f"An unexpected error occurred: {str(e)}")
42
  return None
43
+ return wrapper
44
+
45
+ def handle_ai_model_exceptions(func: Callable) -> Callable:
46
+ """
47
+ Decorator to handle AI model-related exceptions gracefully
48
+ """
49
+ @functools.wraps(func)
50
+ def wrapper(*args, **kwargs):
51
+ try:
52
+ return func(*args, **kwargs)
53
+ except (AIModelError, ValidationError) as e:
54
+ log_error(logger, f"AI model error in {func.__name__}: {str(e)}")
55
+ print(f"AI Model Error: {str(e)}")
56
+ return None
57
+ except Exception as e:
58
+ log_error(logger, f"Unexpected AI model error in {func.__name__}: {str(e)}")
59
+ log_error(logger, f"Traceback: {traceback.format_exc()}")
60
+ print(f"An unexpected AI model error occurred: {str(e)}")
61
+ return None
62
+ return wrapper
63
+
64
+ def handle_exceptions(func: Callable) -> Callable:
65
+ """
66
+ General exception handler decorator
67
+ """
68
+ @functools.wraps(func)
69
+ def wrapper(*args, **kwargs):
70
+ try:
71
+ return func(*args, **kwargs)
72
+ except Exception as e:
73
+ log_error(logger, f"Exception in {func.__name__}: {str(e)}")
74
+ log_error(logger, f"Traceback: {traceback.format_exc()}")
75
+ return None
76
  return wrapper
77
 
78
  def validate_data(data: Any, data_type: str) -> bool:
79
  """
80
  Validate data based on expected type
 
 
 
 
 
 
 
 
 
 
81
  """
82
  if data is None:
83
  raise ValidationError(f"Data cannot be None for type: {data_type}")
84
+
85
  if data_type == "dataframe":
86
  try:
87
  import pandas as pd
 
91
  raise ValidationError("DataFrame cannot be empty")
92
  except ImportError:
93
  raise ValidationError("pandas not available for DataFrame validation")
94
+
95
  elif data_type == "list":
96
  if not isinstance(data, list):
97
  raise ValidationError("Expected list")
98
  if len(data) == 0:
99
  raise ValidationError("List cannot be empty")
100
+
101
  elif data_type == "dict":
102
  if not isinstance(data, dict):
103
  raise ValidationError("Expected dictionary")
104
  if len(data) == 0:
105
  raise ValidationError("Dictionary cannot be empty")
106
+
107
  return True
108
 
109
  def show_error(message: str):
 
136
  except ImportError:
137
  print(f"SUCCESS: {message}")
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  def safe_get(dictionary: dict, key: str, default: Any = None) -> Any:
140
  """
141
  Safely get value from dictionary
 
 
 
 
 
 
 
 
142
  """
143
  try:
144
  return dictionary.get(key, default)
145
  except (AttributeError, TypeError):
146
  logger.warning(f"safe_get failed for key '{key}' - dictionary is not dict-like")
147
+ return default