diff --git a/src/main/java/com/youlai/boot/common/exception/MsgException.java b/src/main/java/com/youlai/boot/common/exception/MsgException.java new file mode 100644 index 0000000..f8e719c --- /dev/null +++ b/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(); + } +} diff --git a/src/main/java/com/youlai/boot/common/result/ResultCode.java b/src/main/java/com/youlai/boot/common/result/ResultCode.java index a0833d1..189e7bc 100644 --- a/src/main/java/com/youlai/boot/common/result/ResultCode.java +++ b/src/main/java/com/youlai/boot/common/result/ResultCode.java @@ -53,10 +53,11 @@ import java.io.Serializable; public enum ResultCode implements IResultCode, Serializable { SUCCESS("00000", "成功"), - + /** 一级宏观错误码:用户端错误(由客户端输入/认证/权限/请求方式等引起,需客户端配合修正) */ USER_ERROR("A0001", "用户端错误"), - + MSG_ERROR("A0002", "需反馈给用户端的提示"), + /** 二级宏观错误码:用户端具体错误(按号段细分,便于定位是注册/登录/令牌/参数/防重等问题) */ @@ -98,7 +99,7 @@ public enum ResultCode implements IResultCode, Serializable { /** A07xx:文件处理异常 */ UPLOAD_FILE_EXCEPTION("A0700", "上传文件异常"), DELETE_FILE_EXCEPTION("A0710", "删除文件异常"), - + /** 一级宏观错误码:系统端错误(服务端内部异常/超时/不可用等,需后端排查修复) */ SYSTEM_ERROR("B0001", "系统执行出错"), diff --git a/src/main/java/com/youlai/boot/framework/web/advice/GlobalExceptionHandler.java b/src/main/java/com/youlai/boot/framework/web/advice/GlobalExceptionHandler.java index fb0e25d..131578f 100644 --- a/src/main/java/com/youlai/boot/framework/web/advice/GlobalExceptionHandler.java +++ b/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 Result handleMsgException(MsgException e) { + log.error("msg exception", e); + return Result.failed(ResultCode.MSG_ERROR, e.getMessage()); + } + /** * 处理所有未捕获的异常 *

diff --git a/src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalServiceImpl.java b/src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalServiceImpl.java index ec7432d..1682c8b 100644 --- a/src/main/java/com/youlai/boot/mini/service/impl/StrayAnimalServiceImpl.java +++ b/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 images, List 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() .eq(MiniStrayAnimalNote::getUuid, deleteStrayAnimalNoteMediaDTO.getNoteUuid())); if (note == null) { - throw new BusinessException("笔记不存在"); + throw new MsgException("笔记不存在"); } LambdaQueryWrapper queryWrapper = @@ -333,7 +333,7 @@ public class StrayAnimalServiceImpl extends ServiceImpl images, List videos) { if ((images == null || images.isEmpty()) && (videos == null || videos.isEmpty())) { - throw new BusinessException("需要上传媒体资源"); + throw new MsgException("需要上传媒体资源"); } MiniStrayAnimalNote note = miniStrayAnimalNoteMapper.selectOne( new LambdaQueryWrapper() @@ -341,16 +341,16 @@ public class StrayAnimalServiceImpl extends ServiceImpl 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 + "个"); } }