Browse Source

反馈给用户提示,异常封装

master
review512jwy@163.com 1 month ago
parent
commit
56621ed02e
  1. 45
      src/main/java/com/youlai/boot/common/exception/MsgException.java
  2. 1
      src/main/java/com/youlai/boot/common/result/ResultCode.java
  3. 8
      src/main/java/com/youlai/boot/framework/web/advice/GlobalExceptionHandler.java
  4. 26
      src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalServiceImpl.java

45
src/main/java/com/youlai/boot/common/exception/MsgException.java

@ -0,0 +1,45 @@
package com.youlai.boot.common.exception;
import com.youlai.boot.common.result.IResultCode;
import lombok.Getter;
import org.slf4j.helpers.MessageFormatter;
/**
* 自定义业务异常
*
* @author Ray
* @since 2022/7/31
*/
@Getter
public class MsgException extends RuntimeException {
public IResultCode resultCode;
public MsgException(IResultCode errorCode) {
super(errorCode.getMsg());
this.resultCode = errorCode;
}
public MsgException(IResultCode errorCode, String message) {
super(message);
this.resultCode = errorCode;
}
public MsgException(String message, Throwable cause) {
super(message, cause);
}
public MsgException(Throwable cause) {
super(cause);
}
public MsgException(String message, Object... args) {
super(formatMessage(message, args));
}
private static String formatMessage(String message, Object... args) {
return MessageFormatter.arrayFormat(message, args).getMessage();
}
}

1
src/main/java/com/youlai/boot/common/result/ResultCode.java

@ -56,6 +56,7 @@ public enum ResultCode implements IResultCode, Serializable {
/** 一级宏观错误码:用户端错误(由客户端输入/认证/权限/请求方式等引起,需客户端配合修正) */
USER_ERROR("A0001", "用户端错误"),
MSG_ERROR("A0002", "需反馈给用户端的提示"),
/** 二级宏观错误码:用户端具体错误(按号段细分,便于定位是注册/登录/令牌/参数/防重等问题) */

8
src/main/java/com/youlai/boot/framework/web/advice/GlobalExceptionHandler.java

@ -1,6 +1,7 @@
package com.youlai.boot.framework.web.advice;
import cn.hutool.core.util.StrUtil;
import com.youlai.boot.common.exception.MsgException;
import tools.jackson.core.JacksonException;
import com.youlai.boot.common.exception.BusinessException;
import com.youlai.boot.common.result.Result;
@ -239,6 +240,13 @@ public class GlobalExceptionHandler {
return Result.failed(e.getMessage());
}
@ExceptionHandler(MsgException.class)
@ResponseStatus(HttpStatus.OK)
public <T> Result<T> handleMsgException(MsgException e) {
log.error("msg exception", e);
return Result.failed(ResultCode.MSG_ERROR, e.getMessage());
}
/**
* 处理所有未捕获的异常
* <p>

26
src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalServiceImpl.java

@ -7,7 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.common.constant.CommonConstants;
import com.youlai.boot.common.exception.BusinessException;
import com.youlai.boot.common.exception.MsgException;
import com.youlai.boot.common.util.CoordinateTransformUtils;
import com.youlai.boot.common.util.FileUtils;
import com.youlai.boot.common.util.JavaVCUtils;
@ -212,18 +212,18 @@ public class StrayAnimalServiceImpl extends ServiceImpl<MiniStrayAnimalMapper, M
private void validateInput(List<MultipartFile> images, List<MultipartFile> videos) {
// 验证必填图片
if (images == null || images.isEmpty()) {
throw new BusinessException("需要上传图片");
throw new MsgException("需要上传图片");
}
// 验证图片数量
if (images.size() > CommonConstants.STRAY_ANIMAL_IMAGE_NUM_LIMIT) {
throw new BusinessException(
throw new MsgException(
"最多只能上传" + CommonConstants.STRAY_ANIMAL_IMAGE_NUM_LIMIT + "张图片"
);
}
// 验证视频数量
if (!CollUtil.isEmpty(videos) && videos.size() > CommonConstants.STRAY_ANIMAL_VIDEO_NUM_LIMIT) {
throw new BusinessException(
throw new MsgException(
"最多只能上传" + CommonConstants.STRAY_ANIMAL_VIDEO_NUM_LIMIT + "个视频"
);
}
@ -239,11 +239,11 @@ public class StrayAnimalServiceImpl extends ServiceImpl<MiniStrayAnimalMapper, M
.eq(MiniStrayAnimal::getDeleted, 0)
);
if (animal == null) {
throw new BusinessException("动物不存在");
throw new MsgException("动物不存在");
}
if (1 == animal.getAuditStatus()) {
throw new BusinessException("审核中无法更改");
throw new MsgException("审核中无法更改");
}
long currentTimestamp = System.currentTimeMillis();
@ -294,7 +294,7 @@ public class StrayAnimalServiceImpl extends ServiceImpl<MiniStrayAnimalMapper, M
MiniStrayAnimalNote note = miniStrayAnimalNoteMapper.selectOne(new LambdaQueryWrapper<MiniStrayAnimalNote>()
.eq(MiniStrayAnimalNote::getUuid, deleteStrayAnimalNoteMediaDTO.getNoteUuid()));
if (note == null) {
throw new BusinessException("笔记不存在");
throw new MsgException("笔记不存在");
}
LambdaQueryWrapper<MiniStrayAnimalNoteMedia> queryWrapper =
@ -333,7 +333,7 @@ public class StrayAnimalServiceImpl extends ServiceImpl<MiniStrayAnimalMapper, M
@Override
public void saveMediaSource(String noteUuid, List<MultipartFile> images, List<MultipartFile> videos) {
if ((images == null || images.isEmpty()) && (videos == null || videos.isEmpty())) {
throw new BusinessException("需要上传媒体资源");
throw new MsgException("需要上传媒体资源");
}
MiniStrayAnimalNote note = miniStrayAnimalNoteMapper.selectOne(
new LambdaQueryWrapper<MiniStrayAnimalNote>()
@ -341,16 +341,16 @@ public class StrayAnimalServiceImpl extends ServiceImpl<MiniStrayAnimalMapper, M
.eq(MiniStrayAnimalNote::getDeleted, 0)
);
if (note == null) {
throw new BusinessException("笔记不存在");
throw new MsgException("笔记不存在");
}
Long animalId = note.getStrayAnimalId();
MiniStrayAnimal miniStrayAnimal = miniStrayAnimalMapper.selectById(animalId);
if (miniStrayAnimal == null) {
throw new BusinessException("动物信息不存在");
throw new MsgException("动物信息不存在");
}
if (1 == miniStrayAnimal.getAuditStatus()){
throw new BusinessException("审核中");
throw new MsgException("审核中");
}
//校验数量有没有超出
@ -361,14 +361,14 @@ public class StrayAnimalServiceImpl extends ServiceImpl<MiniStrayAnimalMapper, M
param.put("mediaType", AnimalNoteMediaTypeEnum.IMAGE.name().toLowerCase());
int imagesExist = miniStrayAnimalNoteMediaMapper.getMediaCountByNoteIdAndType(param);
if (imagesExist+images.size() > CommonConstants.STRAY_ANIMAL_IMAGE_NUM_LIMIT) {
throw new BusinessException("图片总数不能超过" + CommonConstants.STRAY_ANIMAL_IMAGE_NUM_LIMIT + "张");
throw new MsgException("图片总数不能超过" + CommonConstants.STRAY_ANIMAL_IMAGE_NUM_LIMIT + "张");
}
}
if (CollectionUtils.isNotEmpty(videos)){
param.put("mediaType", AnimalNoteMediaTypeEnum.VIDEO.name().toLowerCase());
int videosExist = miniStrayAnimalNoteMediaMapper.getMediaCountByNoteIdAndType(param);
if(videosExist+videos.size() > CommonConstants.STRAY_ANIMAL_VIDEO_NUM_LIMIT){
throw new BusinessException("视频总数不能超过" + CommonConstants.STRAY_ANIMAL_VIDEO_NUM_LIMIT + "个");
throw new MsgException("视频总数不能超过" + CommonConstants.STRAY_ANIMAL_VIDEO_NUM_LIMIT + "个");
}
}

Loading…
Cancel
Save