refactor(#781): 统一应用层异常定义,消除重复异常类 #802

Merged
xiaoxia merged 1 commits from refactor/unified-exceptions into develop 2026-07-24 08:03:11 +08:00
9 changed files with 49 additions and 54 deletions
+1 -4
View File
@@ -12,13 +12,10 @@ from packages.application.recipe.commands import (
UpdateRecipeCommand,
)
from packages.domain.recipe import Recipe, RecipeItem
from packages.domain.exceptions import NotFoundError
from packages.infrastructure.feature_flags import FeatureScope, feature_flags
class NotFoundError(Exception):
pass
class FeatureDisabledError(Exception):
pass
+1 -9
View File
@@ -15,19 +15,11 @@ from packages.application.template.commands import (
ValidateTemplateCommand,
)
from packages.domain.editing_mode import EditingMode
from packages.domain.exceptions import NotFoundError, ValidationError
from packages.domain.template import Template, TemplateCategory, TemplateSegment
from packages.ports.template_repository import TemplateRepositoryPort
class NotFoundError(Exception):
pass
class ValidationError(Exception):
"""业务规则校验失败."""
pass
VALID_MODES = {m.value for m in EditingMode}
VALID_MATERIAL_TYPES = {"人物", "场景"}
@@ -12,6 +12,7 @@ from packages.application.title_library.commands import (
PickTitleCommand,
UpdateTitleLibraryCommand,
)
from packages.domain.exceptions import NotFoundError, QuotaExceededError
from packages.domain.quota import QuotaDimension, quota_checker
from packages.domain.title_library import TitleLibraryItem
@@ -162,15 +163,3 @@ class PickTitleUseCase:
# 随机选一个
return random.choice(pool)
class QuotaExceededError(Exception):
def __init__(self, dimension: str, limit: float, used: float) -> None:
self.dimension = dimension
self.limit = limit
self.used = used
super().__init__(f"Quota exceeded for {dimension}: {used}/{limit}")
class NotFoundError(Exception):
pass
@@ -0,0 +1,7 @@
"""TTS Job 模块公共异常定义。"""
class TTSJobNotFoundError(Exception):
"""TTS 任务未找到。"""
pass
+1 -5
View File
@@ -5,14 +5,10 @@ from __future__ import annotations
from typing import List, Optional
from packages.domain.tts_job import TTSJob
from packages.application.tts_job.exceptions import TTSJobNotFoundError
from packages.ports.tts_job_repository import TTSJobRepository
class TTSJobNotFoundError(Exception):
"""TTS 任务未找到。"""
pass
class CreateTTSJobUseCase:
"""创建 TTS 合成任务。"""
+1 -6
View File
@@ -19,6 +19,7 @@ from typing import Optional
from packages.application.cosyvoice_service import CosyVoiceAuthError, CosyVoiceError, CosyVoiceService
from packages.application.tts_job.audio_merger import AudioMerger
from packages.application.tts_job.exceptions import TTSJobNotFoundError
from packages.application.tts_job.text_splitter import split_text
from packages.domain.tts_job import TTSJob, TTSJobStatus
from packages.ports.tts_job_repository import TTSJobRepository
@@ -43,12 +44,6 @@ class TTSWorkflowError(Exception):
pass
class TTSJobNotFoundError(Exception):
"""TTS 任务未找到。"""
pass
class TTSWorkflowService:
"""TTS 合成工作流编排服务。
@@ -13,15 +13,10 @@ from packages.application.video_share.commands import (
from packages.domain.generated_video import GeneratedVideo
from packages.domain.video_share import VideoShare
from packages.ports.generated_video_repository import GeneratedVideoRepository
from packages.domain.exceptions import NotFoundError
from packages.ports.video_share_repository import VideoShareRepositoryPort
class NotFoundError(Exception):
"""分享记录不存在."""
pass
class VideoNotFoundError(Exception):
"""视频不存在."""
@@ -10,6 +10,7 @@ from packages.application.voice_library.commands import (
CreateVoiceLibraryCommand,
UpdateVoiceLibraryCommand,
)
from packages.domain.exceptions import NotFoundError, QuotaExceededError
from packages.domain.quota import QuotaDimension, quota_checker
from packages.domain.voice_library import VoiceLibraryItem
@@ -117,15 +118,3 @@ class DeleteVoiceLibraryUseCase:
def execute(self, voice_id: str, user_id: str) -> bool:
return self.repository.delete(voice_id, user_id)
class QuotaExceededError(Exception):
def __init__(self, dimension: str, limit: float, used: float) -> None:
self.dimension = dimension
self.limit = limit
self.used = used
super().__init__(f"Quota exceeded for {dimension}: {used}/{limit}")
class NotFoundError(Exception):
pass
+35
View File
@@ -0,0 +1,35 @@
"""
领域层通用异常定义。
所有应用层共享的通用异常在此统一定义,
消除各 use_cases 模块中重复的异常类。
领域特定的异常仍在各模块内定义。
"""
class DomainError(Exception):
"""领域层异常基类。"""
pass
class NotFoundError(DomainError):
"""资源不存在。"""
pass
class ValidationError(DomainError):
"""业务规则校验失败。"""
pass
class QuotaExceededError(DomainError):
"""配额超限。"""
def __init__(self, dimension: str, limit: float, used: float) -> None:
self.dimension = dimension
self.limit = limit
self.used = used
super().__init__(f"Quota exceeded for {dimension}: {used}/{limit}")