-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_utils.py
More file actions
159 lines (123 loc) · 4.17 KB
/
error_utils.py
File metadata and controls
159 lines (123 loc) · 4.17 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Common error handling utilities to reduce code duplication.
"""
import logging
import functools
from typing import Optional, Callable, Any
logger = logging.getLogger(__name__)
def safe_execute(
operation_name: str,
default_return: Any = False,
log_errors: bool = True,
raise_on_error: bool = False,
raise_validation_errors: bool = True,
):
"""
Decorator for safe execution of operations with standardized error handling.
Args:
operation_name: Human-readable name of the operation for logging
default_return: Value to return if operation fails (default: False)
log_errors: Whether to log errors (default: True)
raise_on_error: Whether to re-raise exceptions (default: False)
raise_validation_errors: Whether to re-raise ValueError exceptions (default: True)
"""
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except ValueError as e:
# Always re-raise validation errors unless explicitly disabled
if raise_validation_errors:
raise
if log_errors:
logger.error(f"Error in {operation_name}: {e}")
return default_return
except Exception as e:
if log_errors:
logger.error(f"Error in {operation_name}: {e}")
if raise_on_error:
raise
return default_return
return wrapper
return decorator
def handle_database_error(operation_name: str, error: Exception) -> bool:
"""
Standardized database error handling.
Args:
operation_name: Name of the database operation
error: The exception that occurred
Returns:
bool: False (indicating operation failure)
"""
logger.error(f"Database error in {operation_name}: {error}")
return False
def validate_required_params(**params) -> None:
"""
Validate that required parameters are not None or empty.
Args:
**params: Named parameters to validate
Raises:
ValueError: If any parameter is None or empty string
"""
for name, value in params.items():
if value is None:
raise ValueError(f"Parameter '{name}' is required")
if isinstance(value, str) and not value.strip():
raise ValueError(f"Parameter '{name}' cannot be empty")
def safe_int_conversion(
value: Any,
default: int = 0,
min_val: Optional[int] = None,
max_val: Optional[int] = None,
) -> int:
"""
Safely convert a value to integer with bounds checking.
Args:
value: Value to convert
default: Default value if conversion fails
min_val: Minimum allowed value
max_val: Maximum allowed value
Returns:
int: Converted and validated integer
"""
try:
result = int(value)
if min_val is not None and result < min_val:
return default
if max_val is not None and result > max_val:
return default
return result
except (ValueError, TypeError):
return default
def safe_float_conversion(
value: Any,
default: float = 0.0,
min_val: Optional[float] = None,
max_val: Optional[float] = None,
) -> float:
"""
Safely convert a value to float with bounds checking.
Args:
value: Value to convert
default: Default value if conversion fails
min_val: Minimum allowed value
max_val: Maximum allowed value
Returns:
float: Converted and validated float
"""
try:
result = float(value)
if min_val is not None and result < min_val:
return default
if max_val is not None and result > max_val:
return default
return result
except (ValueError, TypeError):
return default
class ConfigurationError(Exception):
"""Raised when configuration is invalid."""
class DatabaseConnectionError(Exception):
"""Raised when database connection fails."""
class ValidationError(Exception):
"""Raised when input validation fails."""