From 2334a2544c767567cd0b2aed8163b804bf1963fc Mon Sep 17 00:00:00 2001 From: yuliang_guo Date: Sat, 31 Jan 2026 11:15:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dexam=5Fservice?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=B1=BB=E5=AF=BC=E5=85=A5=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将不存在的BusinessException/ErrorCode替换为现有的NotFoundError/ValidationError --- backend/app/services/exam_service.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/backend/app/services/exam_service.py b/backend/app/services/exam_service.py index 0b65fa1..e682cf8 100644 --- a/backend/app/services/exam_service.py +++ b/backend/app/services/exam_service.py @@ -10,7 +10,7 @@ from sqlalchemy import select, func, and_, or_, desc from app.models.exam import Exam, Question, ExamResult from app.models.exam_mistake import ExamMistake from app.models.course import Course, KnowledgePoint -from app.core.exceptions import BusinessException, ErrorCode +from app.core.exceptions import NotFoundError, ValidationError from app.utils.score_distributor import ScoreDistributor @@ -36,7 +36,7 @@ class ExamService: # 检查课程是否存在 course = await db.get(Course, course_id) if not course: - raise BusinessException(error_code=ErrorCode.NOT_FOUND, message="课程不存在") + raise NotFoundError("课程不存在") # 获取该课程的所有可用题目 stmt = select(Question).where( @@ -46,9 +46,7 @@ class ExamService: all_questions = result.scalars().all() if not all_questions: - raise BusinessException( - error_code=ErrorCode.VALIDATION_ERROR, message="该课程暂无题目" - ) + raise ValidationError("该课程暂无题目") # 随机选择题目 selected_questions = random.sample( @@ -120,12 +118,10 @@ class ExamService: exam = result.scalar_one_or_none() if not exam: - raise BusinessException(error_code=ErrorCode.NOT_FOUND, message="考试记录不存在") + raise NotFoundError("考试记录不存在") if exam.status != "started": - raise BusinessException( - error_code=ErrorCode.VALIDATION_ERROR, message="考试已结束或已提交" - ) + raise ValidationError("考试已结束或已提交") # 检查考试是否超时 if datetime.now() > exam.start_time + timedelta( @@ -133,9 +129,7 @@ class ExamService: ): exam.status = "timeout" await db.commit() - raise BusinessException( - error_code=ErrorCode.VALIDATION_ERROR, message="考试已超时" - ) + raise ValidationError("考试已超时") # 处理答案 answers_dict = {ans["question_id"]: ans["answer"] for ans in answers} @@ -223,7 +217,7 @@ class ExamService: exam = result.scalar_one_or_none() if not exam: - raise BusinessException(error_code=ErrorCode.NOT_FOUND, message="考试记录不存在") + raise NotFoundError("考试记录不存在") # 构建返回数据 exam_data = {