|
@@ -1,6 +1,10 @@
|
|
package com.km.common.core.exception;
|
|
package com.km.common.core.exception;
|
|
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import lombok.Getter;
|
|
|
|
+
|
|
import java.io.Serial;
|
|
import java.io.Serial;
|
|
|
|
+import java.text.MessageFormat;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 业务异常
|
|
* 业务异常
|
|
@@ -14,17 +18,18 @@ public final class ServiceException extends RuntimeException {
|
|
/**
|
|
/**
|
|
* 错误码
|
|
* 错误码
|
|
*/
|
|
*/
|
|
|
|
+ @Getter
|
|
private Integer code;
|
|
private Integer code;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 错误提示
|
|
|
|
|
|
+ * 错误参数
|
|
*/
|
|
*/
|
|
- private String message;
|
|
|
|
|
|
+ private Object[] msgParams;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 错误明细,内部调试错误
|
|
* 错误明细,内部调试错误
|
|
*/
|
|
*/
|
|
- private String detailMessage;
|
|
|
|
|
|
+ private String message;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 空构造方法,避免反序列化问题
|
|
* 空构造方法,避免反序列化问题
|
|
@@ -35,24 +40,25 @@ public final class ServiceException extends RuntimeException {
|
|
public ServiceException(String message) {
|
|
public ServiceException(String message) {
|
|
this.message = message;
|
|
this.message = message;
|
|
}
|
|
}
|
|
-
|
|
|
|
public ServiceException(String message, Integer code) {
|
|
public ServiceException(String message, Integer code) {
|
|
this.message = message;
|
|
this.message = message;
|
|
this.code = code;
|
|
this.code = code;
|
|
}
|
|
}
|
|
|
|
|
|
- public String getDetailMessage() {
|
|
|
|
- return detailMessage;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public ServiceException setDetailMessage(String detailMessage) {
|
|
|
|
- this.detailMessage = detailMessage;
|
|
|
|
- return this;
|
|
|
|
|
|
+ public ServiceException(Integer code, String msg, Object... msgParams) {
|
|
|
|
+ this.message = msg;
|
|
|
|
+ this.code = code;
|
|
|
|
+ this.msgParams = msgParams;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public String getMessage() {
|
|
public String getMessage() {
|
|
- return message;
|
|
|
|
|
|
+ if(StrUtil.isNotBlank(getMessage())){
|
|
|
|
+ if(msgParams!=null && msgParams.length>0){
|
|
|
|
+ return MessageFormat.format(message, msgParams);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return super.getMessage(); // 如果不传参数,直接调用父类方法
|
|
}
|
|
}
|
|
|
|
|
|
public ServiceException setMessage(String message) {
|
|
public ServiceException setMessage(String message) {
|
|
@@ -60,7 +66,4 @@ public final class ServiceException extends RuntimeException {
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
- public Integer getCode() {
|
|
|
|
- return code;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|