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
@@ -5,6 +5,7 @@ import com.guo.learningprogresstracker.entity.CommonResult;
import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.exception.ErrorParameterException;
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
import com.guo.learningprogresstracker.exception.OperationFailedException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
@@ -21,16 +22,25 @@ public class GlobalExceptionHandler {
@ExceptionHandler({ErrorParameterException.class})
public CommonResult errorParameterException(ErrorParameterException ex) {
log.warn("参数异常: {}", ex.getMessage(), ex);
return CommonResult.error(ex.getMessage());
}
@ExceptionHandler({NotFindEntitiesException.class})
public CommonResult notFindEntitiesException(NotFindEntitiesException ex) {
log.warn("数据不存在: {}", ex.getMessage(), ex);
return CommonResult.error(ex.getMessage());
}
@ExceptionHandler({OperationFailedException.class})
public CommonResult operationFailedException(OperationFailedException ex) {
log.warn("操作失败: {}", ex.getMessage(), ex);
return CommonResult.error(ex.getMessage());
}
@ExceptionHandler({AppException.class})
public CommonResult AppException(AppException ex) {
log.error("业务异常: {}", ex.getMessage(), ex);
return CommonResult.serverError(ex.getMessage());
}
@@ -49,7 +59,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public CommonResult Exception(Exception ex) {
log.error("系统异常", ex);
return CommonResult.error(ex.getMessage());
return CommonResult.error("操作没有成功,请稍后再试");
}
}