refactor: 优化用户可见错误提示

This commit is contained in:
2026-08-03 23:08:26 +08:00
parent b434520eff
commit 6678c2b8c0
16 changed files with 91 additions and 50 deletions
@@ -50,7 +50,10 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public StudySessionResponse startOrContinueStudySession(String taskNum) throws NotFindEntitiesException, ServiceException {
TaskEntity taskEntity = tasksServiceImpl.getOneOpt(
Wrappers.lambdaQuery(TaskEntity.class).eq(TaskEntity::getTaskNum, taskNum))
.orElseThrow(() -> new NotFindEntitiesException(String.format("[%s]不存在", taskNum)));
.orElseThrow(() -> {
log.warn("[{}]不存在", taskNum);
return new NotFindEntitiesException("这个任务不存在或已被删除");
});
StudySessionsEntity session = this.getOneOpt(Wrappers.<StudySessionsEntity>lambdaQuery()
.eq(StudySessionsEntity::getTaskNum, taskNum)
@@ -88,10 +91,10 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public void pauseStudySession(String sessionNum, LocalDateTime endTime) throws ErrorParameterException, ServiceException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
.orElseThrow(() -> sessionNotFound(sessionNum));
if (StudySessionStateEnum.PAUSED.name().equals(studySessionsEntity.getSessionState())) {
log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复暂停");
throw new ErrorParameterException("会话[" + sessionNum + "]已经暂停,请勿重复暂停");
throw new ErrorParameterException("这次学习会话已经暂停了,不用重复暂停");
}
studySessionsEntity.calculatePointerPosition();
studySessionsEntity.pausedStudySession(endTime);
@@ -104,7 +107,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public String endedStudySession(String sessionNum, String content) throws ErrorParameterException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
.orElseThrow(() -> sessionNotFound(sessionNum));
boolean wasOngoing = StudySessionStateEnum.ONGOING.name().equals(studySessionsEntity.getSessionState());
studySessionsEntity.endedStudySession();
this.updateById(studySessionsEntity);
@@ -122,9 +125,12 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public StudySessionResponse getNotEndedStudySessionByTaskNum(String taskNum) throws ErrorParameterException {
TaskEntity task = tasksServiceImpl.getOneOpt(Wrappers.lambdaQuery(TaskEntity.class)
.eq(TaskEntity::getTaskNum, taskNum))
.orElseThrow(() -> new ErrorParameterException("任务[" + taskNum + "]不存在"));
.orElseThrow(() -> taskNotFound(taskNum));
StudySessionsDto dto = Optional.ofNullable(studyReportsMapper.getNotEndedStudySessionDtoByTaskNum(taskNum))
.orElseThrow(() -> new ErrorParameterException("任务[" + taskNum + "]不存在进行中或暂停中的会话"));
.orElseThrow(() -> {
log.warn("任务[{}]不存在进行中或暂停中的会话", taskNum);
return new ErrorParameterException("这个任务当前没有进行中或已暂停的学习会话");
});
return StudySessionConvert.MAPPER.toStudySessionResponse(dto);
}
@@ -136,7 +142,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
.eq(StudyReportFragmentsEntity::getSessionNum, sessionNum))
.stream().map(StudyReportFragmentsEntity::getContent).collect(Collectors.toCollection(ArrayList::new));
} else {
throw new ErrorParameterException("会话[" + sessionNum + "]不存在");
throw sessionNotFound(sessionNum);
}
}
@@ -145,13 +151,13 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public void continueStudySession(String sessionNum) throws ErrorParameterException, ServiceException {
StudySessionsEntity studySessionsEntity = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
.orElseThrow(() -> sessionNotFound(sessionNum));
if (StudySessionStateEnum.ONGOING.name().equals(studySessionsEntity.getSessionState())) {
log.error("会话[" + studySessionsEntity.getSessionNum() + "]重复开始");
throw new ErrorParameterException("会话[" + sessionNum + "]已经开始,请勿重复开始");
throw new ErrorParameterException("这次学习会话已经开始了,不用重复开始");
} else if (StudySessionStateEnum.ENDED.name().equals(studySessionsEntity.getSessionState())) {
log.error("会话[" + studySessionsEntity.getSessionNum() + "]处于结束状态,不可开始该会话!");
throw new ErrorParameterException("会话[" + sessionNum + "]处于结束状态,不可开始该会话!");
throw new ErrorParameterException("这次学习会话已经结束,无法再次开始");
}
studySessionsEntity.continueStudySession();
this.updateById(studySessionsEntity);
@@ -163,7 +169,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public StudySessionResponse getStudySessionBySessionNum(String sessionNum) throws ErrorParameterException {
StudySessionsEntity session = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
.orElseThrow(() -> sessionNotFound(sessionNum));
return StudySessionConvert.MAPPER.toStudySessionResponse(session);
}
@@ -174,7 +180,7 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
public String generateReportDraft(String sessionNum) throws ErrorParameterException {
StudySessionsEntity session = this.getOneOpt(Wrappers.lambdaQuery(StudySessionsEntity.class)
.eq(StudySessionsEntity::getSessionNum, sessionNum))
.orElseThrow(() -> new ErrorParameterException("会话[" + sessionNum + "]不存在"));
.orElseThrow(() -> sessionNotFound(sessionNum));
ArrayList<String> fragments = getAllFragments(sessionNum);
if (fragments.isEmpty()) {
@@ -262,4 +268,14 @@ public class StudySessionsServiceImpl extends ServiceImpl<StudySessionsMapper, S
return response;
}
private ErrorParameterException sessionNotFound(String sessionNum) {
log.warn("会话[{}]不存在", sessionNum);
return new ErrorParameterException("这次学习会话不存在或已结束");
}
private ErrorParameterException taskNotFound(String taskNum) {
log.warn("任务[{}]不存在", taskNum);
return new ErrorParameterException("这个任务不存在或已被删除");
}
}