Commit 9b1e01fa93b109e48af42825c2175e0f3600c8af
Committed by
Gitee
1 parent
87b75e18
错误码支持格式化输出
Signed-off-by: 杨宇庆 <hiyyq@qq.com>
Showing
1 changed file
with
12 additions
and
3 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/pojo/CommonResult.java
| 1 | 1 | package cn.iocoder.yudao.framework.common.pojo; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.lang.Assert; | |
| 3 | 4 | import cn.iocoder.yudao.framework.common.exception.ErrorCode; |
| 4 | 5 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; |
| 7 | +import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | |
| 6 | 8 | import com.fasterxml.jackson.annotation.JsonIgnore; |
| 7 | 9 | import lombok.Data; |
| 8 | -import org.springframework.util.Assert; | |
| 9 | 10 | |
| 10 | 11 | import java.io.Serializable; |
| 11 | 12 | import java.util.Objects; |
| ... | ... | @@ -41,7 +42,7 @@ public class CommonResult<T> implements Serializable { |
| 41 | 42 | * 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。 |
| 42 | 43 | * |
| 43 | 44 | * @param result 传入的 result 对象 |
| 44 | - * @param <T> 返回的泛型 | |
| 45 | + * @param <T> 返回的泛型 | |
| 45 | 46 | * @return 新的 CommonResult 对象 |
| 46 | 47 | */ |
| 47 | 48 | public static <T> CommonResult<T> error(CommonResult<?> result) { |
| ... | ... | @@ -49,13 +50,21 @@ public class CommonResult<T> implements Serializable { |
| 49 | 50 | } |
| 50 | 51 | |
| 51 | 52 | public static <T> CommonResult<T> error(Integer code, String message) { |
| 52 | - Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!"); | |
| 53 | + Assert.notEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), code, "code 必须是错误的!"); | |
| 53 | 54 | CommonResult<T> result = new CommonResult<>(); |
| 54 | 55 | result.code = code; |
| 55 | 56 | result.msg = message; |
| 56 | 57 | return result; |
| 57 | 58 | } |
| 58 | 59 | |
| 60 | + public static <T> CommonResult<T> error(ErrorCode errorCode, Object... params) { | |
| 61 | + Assert.notEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), errorCode.getCode(), "code 必须是错误的!"); | |
| 62 | + CommonResult<T> result = new CommonResult<>(); | |
| 63 | + result.code = errorCode.getCode(); | |
| 64 | + result.msg = ServiceExceptionUtil.doFormat(errorCode.getCode(), errorCode.getMsg(), params); | |
| 65 | + return result; | |
| 66 | + } | |
| 67 | + | |
| 59 | 68 | public static <T> CommonResult<T> error(ErrorCode errorCode) { |
| 60 | 69 | return error(errorCode.getCode(), errorCode.getMsg()); |
| 61 | 70 | } | ... | ... |