提交
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package com.chaozhanggui.system.cashierservice.exception;
|
||||
|
||||
import com.chaozhanggui.system.cashierservice.annotation.ResultCode;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 一般异常信息的异常,全局捕获会抛出异常信息给前端
|
||||
* @author Djh
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class MsgException extends RuntimeException {
|
||||
|
||||
private final Serializable obj;
|
||||
private final ResultCode code;
|
||||
|
||||
public MsgException(String msg) {
|
||||
this(msg, null);
|
||||
}
|
||||
|
||||
public MsgException(String msg, Serializable obj) {
|
||||
this(ResultCode.FAIL, msg, obj);
|
||||
}
|
||||
|
||||
public MsgException(ResultCode code, String msg, Serializable obj) {
|
||||
super(msg);
|
||||
this.code = code;
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
public static void throwException(String msg) throws MsgException {
|
||||
throw new MsgException(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param result
|
||||
* @param errMsg
|
||||
*
|
||||
* @throws MsgException 为空的时候抛出异常
|
||||
*/
|
||||
public static void check(boolean result, String errMsg) throws MsgException {
|
||||
if (result) {
|
||||
throw new MsgException(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param obj1
|
||||
* @param errMsg
|
||||
*/
|
||||
public static void checkNull(Object obj1, String errMsg) throws MsgException {
|
||||
if (obj1 == null) {
|
||||
throw new MsgException(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkEquals(Object obj1, Object obj2, String errMsg) {
|
||||
if (obj1.equals(obj2)) {
|
||||
throw new MsgException(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkUnequals(Object obj1, Object obj2, String errMsg) {
|
||||
if (!obj1.equals(obj2)) {
|
||||
throw new MsgException(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkNonNull(Object obj1, String errMsg) throws MsgException {
|
||||
if (obj1 != null) {
|
||||
throw new MsgException(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkBlank(String field, String errMsg) throws MsgException {
|
||||
if (StringUtils.isBlank(field)) {
|
||||
throw new MsgException(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> void checkBlank(List<T> dataList, String errMsg) throws MsgException {
|
||||
if (dataList == null || dataList.isEmpty()) {
|
||||
throw new MsgException(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> void checkNonBlank(List<T> dataList, String errMsg) throws MsgException {
|
||||
if (dataList != null && !dataList.isEmpty()) {
|
||||
throw new MsgException(errMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user