Spaces:
Paused
Paused
File size: 10,202 Bytes
4a2ab42 4ae946d 4a2ab42 4ae946d 4a2ab42 4ae946d 4a2ab42 4ae946d 4a2ab42 | 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | """
Standardized Error Handling for Backend Services
Provides consistent error handling patterns across all services
"""
import logging
from contextlib import asynccontextmanager
from dataclasses import dataclass
from enum import Enum
from typing import Any
from fastapi import HTTPException
logger = logging.getLogger(__name__)
class ErrorSeverity(Enum):
LOW = "low"
MEDIUM = "medium"
HIGH = "high"
CRITICAL = "critical"
class ErrorCategory(Enum):
VALIDATION = "validation"
DATABASE = "database"
EXTERNAL_API = "external_api"
AUTHENTICATION = "authentication"
AUTHORIZATION = "authorization"
BUSINESS_LOGIC = "business_logic"
INFRASTRUCTURE = "infrastructure"
UNKNOWN = "unknown"
@dataclass
class ServiceError:
"""Standardized error structure for all services"""
message: str
category: ErrorCategory
severity: ErrorSeverity
error_code: str | None = None
details: dict[str, Any] | None = None
original_error: Exception | None = None
retryable: bool = False
user_friendly_message: str | None = None
def to_dict(self) -> dict[str, Any]:
"""Convert error to dictionary for logging/serialization"""
return {
"message": self.message,
"category": self.category.value,
"severity": self.severity.value,
"error_code": self.error_code,
"details": self.details,
"retryable": self.retryable,
"user_friendly_message": self.user_friendly_message,
}
class ServiceErrorHandler:
"""Centralized error handler for consistent error processing"""
@staticmethod
def create_error(
message: str,
category: ErrorCategory,
severity: ErrorSeverity,
error_code: str | None = None,
details: dict[str, Any] | None = None,
original_error: Exception | None = None,
retryable: bool = False,
user_friendly_message: str | None = None,
) -> ServiceError:
"""Create a standardized service error"""
return ServiceError(
message=message,
category=category,
severity=severity,
error_code=error_code,
details=details,
original_error=original_error,
retryable=retryable,
user_friendly_message=user_friendly_message,
)
@staticmethod
def handle_database_error(error: Exception, operation: str) -> ServiceError:
"""Handle database-related errors"""
error_str = str(error).lower()
if "connection" in error_str or "timeout" in error_str:
return ServiceError(
message=f"Database connection error during {operation}",
category=ErrorCategory.DATABASE,
severity=ErrorSeverity.HIGH,
error_code="DB_CONNECTION_ERROR",
original_error=error,
retryable=True,
user_friendly_message="Database temporarily unavailable. Please try again.",
)
elif "constraint" in error_str or "integrity" in error_str:
return ServiceError(
message=f"Database constraint violation during {operation}",
category=ErrorCategory.VALIDATION,
severity=ErrorSeverity.MEDIUM,
error_code="DB_CONSTRAINT_ERROR",
original_error=error,
retryable=False,
user_friendly_message="Invalid data provided. Please check your input.",
)
else:
return ServiceError(
message=f"Database error during {operation}",
category=ErrorCategory.DATABASE,
severity=ErrorSeverity.HIGH,
error_code="DB_GENERIC_ERROR",
original_error=error,
retryable=True,
user_friendly_message="A database error occurred. Please try again.",
)
@staticmethod
def handle_external_api_error(error: Exception, service_name: str) -> ServiceError:
"""Handle external API errors"""
error_str = str(error).lower()
if "timeout" in error_str or "connection" in error_str:
return ServiceError(
message=f"External API timeout for {service_name}",
category=ErrorCategory.EXTERNAL_API,
severity=ErrorSeverity.MEDIUM,
error_code="API_TIMEOUT",
original_error=error,
retryable=True,
user_friendly_message=f"{service_name} is currently unavailable. Please try again later.",
)
elif "rate limit" in error_str or "429" in error_str:
return ServiceError(
message=f"Rate limit exceeded for {service_name}",
category=ErrorCategory.EXTERNAL_API,
severity=ErrorSeverity.MEDIUM,
error_code="API_RATE_LIMIT",
original_error=error,
retryable=True,
user_friendly_message="Service is temporarily busy. Please try again in a few minutes.",
)
else:
return ServiceError(
message=f"External API error for {service_name}",
category=ErrorCategory.EXTERNAL_API,
severity=ErrorSeverity.HIGH,
error_code="API_GENERIC_ERROR",
original_error=error,
retryable=True,
user_friendly_message="External service error. Please try again later.",
)
@staticmethod
def handle_validation_error(
error: Exception, field: str | None = None
) -> ServiceError:
"""Handle validation errors"""
return ServiceError(
message=f"Validation error{' for ' + field if field else ''}",
category=ErrorCategory.VALIDATION,
severity=ErrorSeverity.LOW,
error_code="VALIDATION_ERROR",
original_error=error,
retryable=False,
user_friendly_message="Please check your input and try again.",
)
@staticmethod
def handle_authentication_error(error: Exception) -> ServiceError:
"""Handle authentication errors"""
return ServiceError(
message="Authentication failed",
category=ErrorCategory.AUTHENTICATION,
severity=ErrorSeverity.MEDIUM,
error_code="AUTH_FAILED",
original_error=error,
retryable=False,
user_friendly_message="Authentication failed. Please check your credentials.",
)
@staticmethod
def handle_authorization_error(
error: Exception, resource: str | None = None
) -> ServiceError:
"""Handle authorization errors"""
return ServiceError(
message=f"Authorization failed{' for ' + resource if resource else ''}",
category=ErrorCategory.AUTHORIZATION,
severity=ErrorSeverity.MEDIUM,
error_code="AUTHZ_FAILED",
original_error=error,
retryable=False,
user_friendly_message="You don't have permission to perform this action.",
)
@staticmethod
def log_and_raise_http_error(service_error: ServiceError) -> None:
"""Log error and raise appropriate HTTP exception"""
# Log the error with structured data
log_data = {
"message": service_error.message,
"category": service_error.category.value,
"severity": service_error.severity.value,
"error_code": service_error.error_code,
"retryable": service_error.retryable,
}
if service_error.details:
log_data["details"] = service_error.details
if service_error.original_error:
log_data["original_error"] = str(service_error.original_error)
# Choose log level based on severity
if service_error.severity == ErrorSeverity.CRITICAL:
logger.critical("Service error", extra=log_data)
elif service_error.severity == ErrorSeverity.HIGH:
logger.error("Service error", extra=log_data)
elif service_error.severity == ErrorSeverity.MEDIUM:
logger.warning("Service error", extra=log_data)
else:
logger.info("Service error", extra=log_data)
# Raise HTTP exception with user-friendly message
status_code = ServiceErrorHandler._get_http_status_code(service_error)
raise HTTPException(
status_code=status_code,
detail=service_error.user_friendly_message or service_error.message,
)
@staticmethod
def _get_http_status_code(service_error: ServiceError) -> int:
"""Map service error to HTTP status code"""
from fastapi import status
category_status_map = {
ErrorCategory.VALIDATION: status.HTTP_400_BAD_REQUEST,
ErrorCategory.AUTHENTICATION: status.HTTP_401_UNAUTHORIZED,
ErrorCategory.AUTHORIZATION: status.HTTP_403_FORBIDDEN,
ErrorCategory.DATABASE: status.HTTP_503_SERVICE_UNAVAILABLE,
ErrorCategory.EXTERNAL_API: status.HTTP_502_BAD_GATEWAY,
ErrorCategory.BUSINESS_LOGIC: status.HTTP_422_UNPROCESSABLE_ENTITY,
ErrorCategory.INFRASTRUCTURE: status.HTTP_503_SERVICE_UNAVAILABLE,
}
return category_status_map.get(
service_error.category, status.HTTP_500_INTERNAL_SERVER_ERROR
)
# Context manager for service operations
@asynccontextmanager
async def service_operation_context(
operation_name: str, category: ErrorCategory = ErrorCategory.UNKNOWN
):
"""Context manager for service operations with standardized error handling"""
try:
yield
except Exception as e:
error = ServiceError(
message=f"Error in {operation_name}",
category=category,
severity=ErrorSeverity.MEDIUM,
original_error=e,
retryable=True,
)
ServiceErrorHandler.log_and_raise_http_error(error)
# Global error handler instance
error_handler = ServiceErrorHandler()
|