197 lines
4.0 KiB
Python
197 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
统一异常定义
|
|
提供标准化的异常类型
|
|
"""
|
|
|
|
from typing import Optional, Dict, Any
|
|
|
|
|
|
class BaseAppException(Exception):
|
|
"""应用基础异常"""
|
|
|
|
error_code: str = "UNKNOWN_ERROR"
|
|
status_code: int = 500
|
|
|
|
def __init__(
|
|
self,
|
|
message: str,
|
|
error_code: Optional[str] = None,
|
|
details: Optional[Dict[str, Any]] = None
|
|
):
|
|
super().__init__(message)
|
|
self.message = message
|
|
if error_code:
|
|
self.error_code = error_code
|
|
self.details = details or {}
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
"""转换为字典"""
|
|
return {
|
|
"error": True,
|
|
"error_code": self.error_code,
|
|
"message": self.message,
|
|
"details": self.details
|
|
}
|
|
|
|
|
|
# ========== 配置相关 ==========
|
|
|
|
class ConfigError(BaseAppException):
|
|
"""配置错误"""
|
|
error_code = "CONFIG_ERROR"
|
|
status_code = 500
|
|
|
|
|
|
class ConfigNotFoundError(ConfigError):
|
|
"""配置未找到"""
|
|
error_code = "CONFIG_NOT_FOUND"
|
|
|
|
|
|
# ========== 验证相关 ==========
|
|
|
|
class ValidationError(BaseAppException):
|
|
"""验证错误"""
|
|
error_code = "VALIDATION_ERROR"
|
|
status_code = 400
|
|
|
|
|
|
class MissingParameterError(ValidationError):
|
|
"""缺少参数"""
|
|
error_code = "MISSING_PARAMETER"
|
|
|
|
|
|
class InvalidParameterError(ValidationError):
|
|
"""无效参数"""
|
|
error_code = "INVALID_PARAMETER"
|
|
|
|
|
|
# ========== 资源相关 ==========
|
|
|
|
class ResourceError(BaseAppException):
|
|
"""资源错误"""
|
|
error_code = "RESOURCE_ERROR"
|
|
status_code = 404
|
|
|
|
|
|
class ResourceNotFoundError(ResourceError):
|
|
"""资源未找到"""
|
|
error_code = "RESOURCE_NOT_FOUND"
|
|
|
|
|
|
class ResourceExistsError(ResourceError):
|
|
"""资源已存在"""
|
|
error_code = "RESOURCE_EXISTS"
|
|
status_code = 409
|
|
|
|
|
|
# ========== 引擎相关 ==========
|
|
|
|
class EngineError(BaseAppException):
|
|
"""引擎错误"""
|
|
error_code = "ENGINE_ERROR"
|
|
status_code = 500
|
|
|
|
|
|
class EngineNotFoundError(EngineError):
|
|
"""引擎未找到"""
|
|
error_code = "ENGINE_NOT_FOUND"
|
|
status_code = 404
|
|
|
|
|
|
class EngineExecutionError(EngineError):
|
|
"""引擎执行错误"""
|
|
error_code = "ENGINE_EXECUTION_ERROR"
|
|
|
|
|
|
class EngineTimeoutError(EngineError):
|
|
"""引擎超时"""
|
|
error_code = "ENGINE_TIMEOUT"
|
|
status_code = 504
|
|
|
|
|
|
# ========== AI 相关 ==========
|
|
|
|
class AIError(BaseAppException):
|
|
"""AI 服务错误"""
|
|
error_code = "AI_ERROR"
|
|
status_code = 502
|
|
|
|
|
|
class AIRateLimitError(AIError):
|
|
"""AI 限流"""
|
|
error_code = "AI_RATE_LIMIT"
|
|
status_code = 429
|
|
|
|
|
|
class AIContentFilterError(AIError):
|
|
"""AI 内容过滤"""
|
|
error_code = "AI_CONTENT_FILTER"
|
|
|
|
|
|
class AIResponseParseError(AIError):
|
|
"""AI 响应解析错误"""
|
|
error_code = "AI_RESPONSE_PARSE_ERROR"
|
|
|
|
|
|
# ========== 数据库相关 ==========
|
|
|
|
class DatabaseError(BaseAppException):
|
|
"""数据库错误"""
|
|
error_code = "DATABASE_ERROR"
|
|
status_code = 500
|
|
|
|
|
|
class DatabaseConnectionError(DatabaseError):
|
|
"""数据库连接错误"""
|
|
error_code = "DATABASE_CONNECTION_ERROR"
|
|
|
|
|
|
# ========== 文件相关 ==========
|
|
|
|
class FileError(BaseAppException):
|
|
"""文件错误"""
|
|
error_code = "FILE_ERROR"
|
|
status_code = 500
|
|
|
|
|
|
class FileNotFoundError(FileError):
|
|
"""文件未找到"""
|
|
error_code = "FILE_NOT_FOUND"
|
|
status_code = 404
|
|
|
|
|
|
class FileUploadError(FileError):
|
|
"""文件上传错误"""
|
|
error_code = "FILE_UPLOAD_ERROR"
|
|
|
|
|
|
# ========== 网络相关 ==========
|
|
|
|
class NetworkError(BaseAppException):
|
|
"""网络错误"""
|
|
error_code = "NETWORK_ERROR"
|
|
status_code = 502
|
|
|
|
|
|
class TimeoutError(NetworkError):
|
|
"""超时错误"""
|
|
error_code = "TIMEOUT_ERROR"
|
|
status_code = 504
|
|
|
|
|
|
# ========== 权限相关 ==========
|
|
|
|
class AuthError(BaseAppException):
|
|
"""认证错误"""
|
|
error_code = "AUTH_ERROR"
|
|
status_code = 401
|
|
|
|
|
|
class PermissionDeniedError(AuthError):
|
|
"""权限拒绝"""
|
|
error_code = "PERMISSION_DENIED"
|
|
status_code = 403
|