mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 10:18:42 +08:00 
			
		
		
		
	【重构】新增 yudao-spring-boot-starter-biz-error-code 错误码组件
				
					
				
			This commit is contained in:
		| @@ -0,0 +1,60 @@ | ||||
| package cn.iocoder.yudao.framework.common.exception; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; | ||||
| import lombok.Data; | ||||
| import lombok.EqualsAndHashCode; | ||||
|  | ||||
| /** | ||||
|  * 服务器异常 Exception | ||||
|  */ | ||||
| @Data | ||||
| @EqualsAndHashCode(callSuper = true) | ||||
| public final class ServerException extends RuntimeException { | ||||
|  | ||||
|     /** | ||||
|      * 全局错误码 | ||||
|      * | ||||
|      * @see GlobalErrorCodeConstants | ||||
|      */ | ||||
|     private Integer code; | ||||
|     /** | ||||
|      * 错误提示 | ||||
|      */ | ||||
|     private String message; | ||||
|  | ||||
|     /** | ||||
|      * 空构造方法,避免反序列化问题 | ||||
|      */ | ||||
|     public ServerException() { | ||||
|     } | ||||
|  | ||||
|     public ServerException(ErrorCode errorCode) { | ||||
|         this.code = errorCode.getCode(); | ||||
|         this.message = errorCode.getMsg(); | ||||
|     } | ||||
|  | ||||
|     public ServerException(Integer code, String message) { | ||||
|         this.code = code; | ||||
|         this.message = message; | ||||
|     } | ||||
|  | ||||
|     public Integer getCode() { | ||||
|         return code; | ||||
|     } | ||||
|  | ||||
|     public ServerException setCode(Integer code) { | ||||
|         this.code = code; | ||||
|         return this; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String getMessage() { | ||||
|         return message; | ||||
|     } | ||||
|  | ||||
|     public ServerException setMessage(String message) { | ||||
|         this.message = message; | ||||
|         return this; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -36,9 +36,15 @@ public interface GlobalErrorCodeConstants { | ||||
|  | ||||
|     ErrorCode UNKNOWN = new ErrorCode(999, "未知错误"); | ||||
|  | ||||
|    static boolean isMatch(Integer code) { | ||||
|     /** | ||||
|      * 是否为服务端错误,参考 HTTP 5XX 错误码段 | ||||
|      * | ||||
|      * @param code 错误码 | ||||
|      * @return 是否 | ||||
|      */ | ||||
|    static boolean isServerErrorCode(Integer code) { | ||||
|        return code != null | ||||
|                && code >= SUCCESS.getCode() && code <= UNKNOWN.getCode(); | ||||
|                && code >= INTERNAL_SERVER_ERROR.getCode() && code <= INTERNAL_SERVER_ERROR.getCode() + 99; | ||||
|    } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| package cn.iocoder.yudao.framework.common.pojo; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.exception.ErrorCode; | ||||
| import cn.iocoder.yudao.framework.common.exception.ServerException; | ||||
| import cn.iocoder.yudao.framework.common.exception.ServiceException; | ||||
| import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; | ||||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||||
| @@ -91,10 +92,24 @@ public class CommonResult<T> implements Serializable { | ||||
|         if (isSuccess()) { | ||||
|             return; | ||||
|         } | ||||
|         // 服务端异常 | ||||
|         if (GlobalErrorCodeConstants.isServerErrorCode(code)) { | ||||
|             throw new ServerException(code, msg); | ||||
|         } | ||||
|         // 业务异常 | ||||
|         throw new ServiceException(code, msg); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常 | ||||
|      * 如果没有,则返回 {@link #data} 数据 | ||||
|      */ | ||||
|     @JsonIgnore // 避免 jackson 序列化 | ||||
|     public T getCheckedData() { | ||||
|         checkError(); | ||||
|         return data; | ||||
|     } | ||||
|  | ||||
|     public static <T> CommonResult<T> error(ServiceException serviceException) { | ||||
|         return error(serviceException.getCode(), serviceException.getMessage()); | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV