Files
2025-10-14 20:05:29 +08:00

92 lines
2.9 KiB
Python

from fastapi import HTTPException, status
class FileTooLargeException(HTTPException):
"""文件过大异常"""
def __init__(self, file_size: int, max_size: int):
super().__init__(
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
detail={
"code": "FILE_TOO_LARGE",
"message": f"文件大小 {file_size} 字节超过限制 {max_size} 字节",
"file_size": file_size,
"max_size": max_size
}
)
class StorageQuotaExceededException(HTTPException):
"""存储配额超限异常"""
def __init__(self, used_space: int, quota: int, required_space: int):
super().__init__(
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
detail={
"code": "STORAGE_QUOTA_EXCEEDED",
"message": f"存储空间不足。已使用: {used_space} 字节,配额: {quota} 字节,需要: {required_space} 字节",
"used_space": used_space,
"quota": quota,
"required_space": required_space
}
)
class FileAlreadyExistsException(HTTPException):
"""文件已存在异常"""
def __init__(self, filename: str):
super().__init__(
status_code=status.HTTP_409_CONFLICT,
detail={
"code": "FILE_ALREADY_EXISTS",
"message": f"文件 '{filename}' 已存在",
"filename": filename
}
)
class FileNotFoundException(HTTPException):
"""文件未找到异常"""
def __init__(self):
super().__init__(
status_code=status.HTTP_404_NOT_FOUND,
detail={
"code": "FILE_NOT_FOUND",
"message": "文件不存在"
}
)
class InvalidFileTypeException(HTTPException):
"""无效文件类型异常"""
def __init__(self, file_extension: str):
super().__init__(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"code": "INVALID_FILE_TYPE",
"message": f"不支持的文件类型: {file_extension}",
"file_extension": file_extension
}
)
class FileUploadException(HTTPException):
"""文件上传异常"""
def __init__(self, message: str):
super().__init__(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail={
"code": "FILE_UPLOAD_FAILED",
"message": f"文件上传失败: {message}"
}
)
class FileDeleteException(HTTPException):
"""文件删除异常"""
def __init__(self, message: str):
super().__init__(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail={
"code": "FILE_DELETE_FAILED",
"message": f"文件删除失败: {message}"
}
)