init
This commit is contained in:
133
src/main/java/com/senmesh/common/exception/util/Assert.java
Normal file
133
src/main/java/com/senmesh/common/exception/util/Assert.java
Normal file
@@ -0,0 +1,133 @@
|
||||
package com.senmesh.common.exception.util;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.senmesh.common.constant.CommonConstants;
|
||||
import com.senmesh.common.exception.BusinessException;
|
||||
import com.senmesh.common.util.JsonUtils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* 断言
|
||||
*
|
||||
* @author sr on 2020-09-02
|
||||
*/
|
||||
@Slf4j
|
||||
public class Assert {
|
||||
|
||||
private Assert() {
|
||||
}
|
||||
|
||||
private static void dealLog(String message, Object... logData) {
|
||||
if (logData.length > 0) {
|
||||
log.info(new StringBuilder().append(message).append(CommonConstants.COLON)
|
||||
.append(JsonUtils.toJsonString(logData)).toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* true正常,fasle抛BusinessException
|
||||
*
|
||||
* @param expression
|
||||
* @param message
|
||||
* @author sr on 2020-09-02
|
||||
*/
|
||||
public static void isTrue(boolean expression, String message, Object... logData) {
|
||||
if (!expression) {
|
||||
dealLog(message, logData);
|
||||
throw new BusinessException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* true正常,fasle抛BusinessException
|
||||
*
|
||||
* @param expression 表达式
|
||||
* @param message 消息
|
||||
*/
|
||||
public static void isTrue(boolean expression, Supplier<String> message) {
|
||||
if (!expression) {
|
||||
throw new BusinessException(message.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* true正常,fasle抛指定异常
|
||||
* @param expression
|
||||
* @param e
|
||||
* @author sr on 2021-06-18
|
||||
*/
|
||||
public static void isTrue(boolean expression, RuntimeException e) {
|
||||
if (!expression) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* null正常,notNull抛BusinessException
|
||||
*
|
||||
* @param object
|
||||
* @param message
|
||||
* @author sr on 2020-09-02
|
||||
*/
|
||||
public static void isNull(@Nullable Object object, String message, Object... logData) {
|
||||
if (object != null) {
|
||||
dealLog(message, logData);
|
||||
throw new BusinessException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* notNull正常,null抛BusinessException
|
||||
*
|
||||
* @param object
|
||||
* @param message
|
||||
* @author sr on 2020-09-02
|
||||
*/
|
||||
public static void notNull(@Nullable Object object, String message, Object... logData) {
|
||||
if (object == null) {
|
||||
dealLog(message, logData);
|
||||
throw new BusinessException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* notNull正常,null抛BusinessException
|
||||
*
|
||||
* @param object 对象
|
||||
* @param message 消息
|
||||
*/
|
||||
public static void notNull(@Nullable Object object, Supplier<String> message) {
|
||||
if (object == null) {
|
||||
throw new BusinessException(message.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* notBlank正常,blank抛BusinessException
|
||||
*
|
||||
* @param str
|
||||
* @param message
|
||||
* @author sr on 2021-01-06
|
||||
*/
|
||||
public static void notBlank(String str, String message, Object... logData) {
|
||||
if (StringUtils.isBlank(str)) {
|
||||
dealLog(message, logData);
|
||||
throw new BusinessException(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void notEmpty(@Nullable Collection<?> collection, String message) {
|
||||
if (CollectionUtils.isEmpty(collection)) {
|
||||
throw new BusinessException(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.senmesh.common.exception.util;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.senmesh.common.exception.IDevopsProvider;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 运维工具类
|
||||
* @author sr on 2022-03-01
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DevopsUtils {
|
||||
|
||||
private static IDevopsProvider devopsProvider;
|
||||
|
||||
@Autowired(required = false)
|
||||
public void setDevopsProvider(IDevopsProvider devopsProvider) {
|
||||
DevopsUtils.devopsProvider = devopsProvider;
|
||||
}
|
||||
|
||||
public static void deal(String msg) {
|
||||
if (devopsProvider == null) {
|
||||
log.error("no devops provider, error:{}", msg);
|
||||
return ;
|
||||
}
|
||||
devopsProvider.deal(msg);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.senmesh.common.exception.util;
|
||||
|
||||
import com.senmesh.common.exception.BusinessException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 异常工具类
|
||||
* @author sr on 2019-10-14
|
||||
*/
|
||||
@Slf4j
|
||||
public class ExceptionUtils {
|
||||
|
||||
/**
|
||||
* @Description 抛异常
|
||||
* @Author chenyun.huang
|
||||
* @Date 2020/3/27 15:06
|
||||
**/
|
||||
public static void throwBusinessException(String msg) {
|
||||
ExceptionUtils.throwBusinessException(true,msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据判断抛异常
|
||||
* @param isThrow
|
||||
* @param msg 异常信息
|
||||
* @author sr on 2019-10-14
|
||||
*/
|
||||
public static void throwBusinessException(boolean isThrow, String msg) {
|
||||
if (isThrow) {
|
||||
throw new BusinessException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据判断抛异常
|
||||
* @param isThrow
|
||||
* @param errorMsg 异常信息
|
||||
* @param logMsg 日志信息(info级)
|
||||
* @author sr on 2019-12-18
|
||||
*/
|
||||
public static void throwBusinessException(boolean isThrow, String errorMsg, String logMsg) {
|
||||
if (isThrow) {
|
||||
log.info(logMsg);
|
||||
throw new BusinessException(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.senmesh.common.exception.util;
|
||||
|
||||
import com.senmesh.common.util.RandomUtils;
|
||||
|
||||
/**
|
||||
* uuid工具类
|
||||
* {@link RandomUtils}
|
||||
* @author sr on 2019-12-10
|
||||
*/
|
||||
@Deprecated
|
||||
public class UUIDUtils {
|
||||
|
||||
/**
|
||||
* 生成uuid,去掉-
|
||||
* @return
|
||||
* @author sr on 2019-12-10
|
||||
*/
|
||||
public static String generateUUID() {
|
||||
return RandomUtils.generateUUID();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user