Spaces:
Sleeping
Sleeping
File size: 4,224 Bytes
545ce24 |
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 |
from typing import Optional, List, Dict, Any
from pydantic import BaseModel, Field, ConfigDict
from enum import Enum
class ErrorLevel(str, Enum):
INFO = "info"
WARNING = "warning"
ERROR = "error"
CRITICAL = "critical"
class ErrorDetail(BaseModel):
"""Base model for error details"""
code: str = Field(..., description="Error code for the specific error")
message: str = Field(..., description="Human readable error message")
level: ErrorLevel = Field(default=ErrorLevel.ERROR, description="Severity level of the error")
context: Optional[Dict[str, Any]] = Field(default=None, description="Additional context about the error")
class GitHubURLError(ErrorDetail):
"""Model for GitHub URL related errors"""
model_config = ConfigDict(json_schema_extra={
"example": {
"code": "INVALID_GITHUB_URL",
"message": "The provided URL is not a valid GitHub repository URL",
"level": ErrorLevel.ERROR,
"context": {"url": "https://invalid-url.com"}
}
})
class TopicAnalysisError(ErrorDetail):
"""Model for topic analysis related errors"""
model_config = ConfigDict(json_schema_extra={
"example": {
"code": "TOPIC_GENERATION_FAILED",
"message": "Failed to generate topics from the content",
"level": ErrorLevel.ERROR,
"context": {"model": "deberta-v3-base", "error": "Model loading failed"}
}
})
class APIResponse(BaseModel):
"""Model for API responses"""
success: bool = Field(default=True, description="Indicates if the operation was successful")
data: Optional[Dict[str, Any]] = Field(default=None, description="Response data when operation is successful")
errors: Optional[List[ErrorDetail]] = Field(default=None, description="List of errors if any occurred")
def model_post_init(self, __context):
"""Post initialization hook to update success status based on errors"""
if self.errors:
self.success = False
class ErrorHandler:
"""Handler for managing and creating error responses"""
@staticmethod
def handle_github_url_error(url: str, error_message: str) -> APIResponse:
"""
Handle GitHub URL related errors
Args:
url: The problematic URL
error_message: Description of the error
Returns:
APIResponse with error details
"""
error = GitHubURLError(
code="INVALID_GITHUB_URL",
message=f"Invalid GitHub URL: {error_message}",
context={"url": url}
)
return APIResponse(success=False, errors=[error])
@staticmethod
def handle_topic_analysis_error(error_message: str, context: Dict[str, Any] = None) -> APIResponse:
"""
Handle topic analysis related errors
Args:
error_message: Description of the error
context: Additional context information
Returns:
APIResponse with error details
"""
error = TopicAnalysisError(
code="TOPIC_GENERATION_FAILED",
message=f"Topic generation failed: {error_message}",
context=context or {}
)
return APIResponse(success=False, errors=[error])
@staticmethod
def handle_file_fetch_error(file_path: str, error_message: str) -> APIResponse:
"""
Handle file fetching related errors
Args:
file_path: Path of the file that failed to fetch
error_message: Description of the error
Returns:
APIResponse with error details
"""
error = ErrorDetail(
code="FILE_FETCH_FAILED",
message=f"Failed to fetch file: {error_message}",
context={"file_path": file_path}
)
return APIResponse(success=False, errors=[error])
@staticmethod
def success_response(data: Dict[str, Any]) -> APIResponse:
"""
Create a success response
Args:
data: The response data to be returned
Returns:
APIResponse with success status and data
"""
return APIResponse(success=True, data=data) |