fix: 修复exam_service异常类导入错误
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
将不存在的BusinessException/ErrorCode替换为现有的NotFoundError/ValidationError
This commit is contained in:
@@ -10,7 +10,7 @@ from sqlalchemy import select, func, and_, or_, desc
|
|||||||
from app.models.exam import Exam, Question, ExamResult
|
from app.models.exam import Exam, Question, ExamResult
|
||||||
from app.models.exam_mistake import ExamMistake
|
from app.models.exam_mistake import ExamMistake
|
||||||
from app.models.course import Course, KnowledgePoint
|
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
|
from app.utils.score_distributor import ScoreDistributor
|
||||||
|
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ class ExamService:
|
|||||||
# 检查课程是否存在
|
# 检查课程是否存在
|
||||||
course = await db.get(Course, course_id)
|
course = await db.get(Course, course_id)
|
||||||
if not course:
|
if not course:
|
||||||
raise BusinessException(error_code=ErrorCode.NOT_FOUND, message="课程不存在")
|
raise NotFoundError("课程不存在")
|
||||||
|
|
||||||
# 获取该课程的所有可用题目
|
# 获取该课程的所有可用题目
|
||||||
stmt = select(Question).where(
|
stmt = select(Question).where(
|
||||||
@@ -46,9 +46,7 @@ class ExamService:
|
|||||||
all_questions = result.scalars().all()
|
all_questions = result.scalars().all()
|
||||||
|
|
||||||
if not all_questions:
|
if not all_questions:
|
||||||
raise BusinessException(
|
raise ValidationError("该课程暂无题目")
|
||||||
error_code=ErrorCode.VALIDATION_ERROR, message="该课程暂无题目"
|
|
||||||
)
|
|
||||||
|
|
||||||
# 随机选择题目
|
# 随机选择题目
|
||||||
selected_questions = random.sample(
|
selected_questions = random.sample(
|
||||||
@@ -120,12 +118,10 @@ class ExamService:
|
|||||||
exam = result.scalar_one_or_none()
|
exam = result.scalar_one_or_none()
|
||||||
|
|
||||||
if not exam:
|
if not exam:
|
||||||
raise BusinessException(error_code=ErrorCode.NOT_FOUND, message="考试记录不存在")
|
raise NotFoundError("考试记录不存在")
|
||||||
|
|
||||||
if exam.status != "started":
|
if exam.status != "started":
|
||||||
raise BusinessException(
|
raise ValidationError("考试已结束或已提交")
|
||||||
error_code=ErrorCode.VALIDATION_ERROR, message="考试已结束或已提交"
|
|
||||||
)
|
|
||||||
|
|
||||||
# 检查考试是否超时
|
# 检查考试是否超时
|
||||||
if datetime.now() > exam.start_time + timedelta(
|
if datetime.now() > exam.start_time + timedelta(
|
||||||
@@ -133,9 +129,7 @@ class ExamService:
|
|||||||
):
|
):
|
||||||
exam.status = "timeout"
|
exam.status = "timeout"
|
||||||
await db.commit()
|
await db.commit()
|
||||||
raise BusinessException(
|
raise ValidationError("考试已超时")
|
||||||
error_code=ErrorCode.VALIDATION_ERROR, message="考试已超时"
|
|
||||||
)
|
|
||||||
|
|
||||||
# 处理答案
|
# 处理答案
|
||||||
answers_dict = {ans["question_id"]: ans["answer"] for ans in answers}
|
answers_dict = {ans["question_id"]: ans["answer"] for ans in answers}
|
||||||
@@ -223,7 +217,7 @@ class ExamService:
|
|||||||
exam = result.scalar_one_or_none()
|
exam = result.scalar_one_or_none()
|
||||||
|
|
||||||
if not exam:
|
if not exam:
|
||||||
raise BusinessException(error_code=ErrorCode.NOT_FOUND, message="考试记录不存在")
|
raise NotFoundError("考试记录不存在")
|
||||||
|
|
||||||
# 构建返回数据
|
# 构建返回数据
|
||||||
exam_data = {
|
exam_data = {
|
||||||
|
|||||||
Reference in New Issue
Block a user