This commit is contained in:
ran.shang
2025-12-29 16:06:48 +08:00
parent b853edcfde
commit ee95dfa2c2
1600 changed files with 6997 additions and 7445 deletions

View File

@@ -0,0 +1,30 @@
package com.senmesh.common.exception;
/**
* 项目所有自定义异常父类
*
* @author guhong
*/
public class AlitaException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 3121871576733025786L;
public AlitaException() {}
public AlitaException(String message) {
super(message);
}
public AlitaException(String message, Throwable cause) {
super(message, cause);
}
public AlitaException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,30 @@
package com.senmesh.common.exception;
/**
* 业务异常
*
* @author guhong
* @date 2019/8/19
*/
public class BusinessException extends AlitaException {
/**
*
*/
private static final long serialVersionUID = -3097174055942519169L;
public BusinessException() {}
public BusinessException(String message) {
super(message);
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
}
public BusinessException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,24 @@
package com.senmesh.common.exception;
import com.senmesh.common.AlitaResult;
import com.senmesh.common.dto.CasTokenDTO;
/**
* 更新token
* @author sr on 2021-07-05
*/
public class ChangeTokenException extends WithResultException{
/** **/
private static final long serialVersionUID = -8897274199463229506L;
public ChangeTokenException(CasTokenDTO casTokenDTO) {
super(AlitaResult.changeToken(casTokenDTO));
}
public CasTokenDTO getCasTokenDTO(){
return (CasTokenDTO) getResult().getErrorData();
}
}

View File

@@ -0,0 +1,24 @@
package com.senmesh.common.exception;
import com.senmesh.common.exception.util.DevopsUtils;
/**
* 运维业务异常
* @author sr on 2022-03-01
*/
public class DevopsException extends BusinessException{
/** **/
private static final long serialVersionUID = 1L;
public DevopsException(String message) {
super(message);
DevopsUtils.deal(message);
}
public DevopsException(String message, Throwable cause) {
super(message, cause);
DevopsUtils.deal(message);
}
}

View File

@@ -0,0 +1,24 @@
package com.senmesh.common.exception;
/**
* 已存在业务异常
* @author sr on 2022-03-01
*/
public class ExistException extends BusinessException{
/** **/
private static final long serialVersionUID = 1L;
public ExistException() {
super();
}
public ExistException(String message) {
super(message);
}
public ExistException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,7 @@
package com.senmesh.common.exception;
public interface IDevopsProvider {
void deal(String msg);
}

View File

@@ -0,0 +1,26 @@
package com.senmesh.common.exception;
/**
* 用户未登录异常
*
* @author guhong
* @date 2019/8/19
*/
public class NoneLoginException extends AlitaException {
public static final String ERROR_MSG = "用户未登录";
/**
*
*/
private static final long serialVersionUID = -7013134475760472324L;
public NoneLoginException(String msg, Throwable t) {
super(msg, t);
}
public NoneLoginException() {
super(ERROR_MSG);
}
}

View File

@@ -0,0 +1,30 @@
package com.senmesh.common.exception;
import com.senmesh.common.AlitaResult;
/**
* 更新token
* @author sr on 2021-07-05
*/
public class WithResultException extends AlitaException{
/** **/
private static final long serialVersionUID = -8897274199463229506L;
private AlitaResult<?> result;
public WithResultException(AlitaResult<?> result) {
super(result.getMsg());
this.result = result;
}
public WithResultException(AlitaResult<?> result, Throwable cause) {
super(result.getMsg(), cause);
this.result = result;
}
public AlitaResult<?> getResult(){
return result;
}
}

View File

@@ -0,0 +1,33 @@
package com.senmesh.common.exception.controller;
import com.senmesh.common.exception.dao.ExceptionLogMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 异常日志查看
*
* @author on 2019-11-05
*/
@RestController
@RequestMapping("/anon/exception")
@Api(tags = "异常日志查看")
public class ExceptionLogController {
@Autowired
private ExceptionLogMapper exceptionLogMapper;
@GetMapping("/{id}")
public String getExceptionMsg(@PathVariable Long id) {
try {
return exceptionLogMapper.selectById(id).getMsg();
} catch (Exception e) {
return "未找到对应id错误";
}
}
}

View File

@@ -0,0 +1,13 @@
package com.senmesh.common.exception.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.senmesh.common.exception.entity.ExceptionLog;
/**
* 异常日志
*
* @author on 2019-11-05
*/
public interface ExceptionLogMapper extends BaseMapper<ExceptionLog> {
}

View File

@@ -0,0 +1,41 @@
package com.senmesh.common.exception.entity;
import java.io.Serializable;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 错误日志
* @author sr on 2019-10-28
*/
@TableName("xs_exception_log")
@Data
@NoArgsConstructor
public class ExceptionLog implements Serializable{
/** **/
private static final long serialVersionUID = 3730091070919812549L;
/** 主键 **/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/** 创建时间 **/
private Date createTime;
/** 错误信息 **/
private String msg;
public ExceptionLog(Date createTime, String msg) {
super();
this.createTime = createTime;
this.msg = msg;
}
}

View File

@@ -0,0 +1,188 @@
package com.senmesh.common.exception.handle;
import com.senmesh.common.util.EnvironmentUtils;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.senmesh.common.AlitaResult;
import com.senmesh.common.constant.CommonConstants;
import com.senmesh.common.exception.BusinessException;
import com.senmesh.common.exception.NoneLoginException;
import com.senmesh.common.exception.WithResultException;
import com.senmesh.common.log.dto.ExceptionLogDTO;
import com.senmesh.common.log.util.LogUtils;
import com.senmesh.common.util.SpringContextHolder;
import com.senmesh.common.util.TracerUtils;
/**
* 全局异常处理
*
* @author guhong
* @date 2019/8/19
*/
@RestControllerAdvice
public class GlobalControllerExceptionHandler {
private static final Logger LOGGER = LoggerFactory
.getLogger(GlobalControllerExceptionHandler.class);
@Value("${spring.application.name:}")
private String applicationName;
/**
* 业务异常
*
* @param ex
*/
@ExceptionHandler(value = {BusinessException.class})
public AlitaResult<String> handleBusinessException(BusinessException ex) {
String traceId = TracerUtils.getTraceId();
String msg;
if (StringUtils.isNotBlank(ex.getMessage()) && ex.getMessage().contains(traceId)) {
msg = ex.getMessage();
}else {
msg = String.format("[%s]操作失败:%s", traceId, ex.getMessage());
}
LOGGER.debug(msg, ex);
AlitaResult<String> alitaResult = AlitaResult.error(AlitaResult.BUSINESS_ERROR_CODE, msg);
alitaResult.setRequestId(traceId);
return alitaResult;
}
/**
* 处理未登录异常
*
* @param ex
*/
@ExceptionHandler(value = {NoneLoginException.class})
public AlitaResult<String> handleNoneLoginException(NoneLoginException ex) {
AlitaResult<String> alitaResult = AlitaResult.unLogin();
alitaResult.setRequestId(TracerUtils.getTraceId());
return alitaResult;
}
/**
* 处理更换token
*
* @param ex
*/
@ExceptionHandler(value = {WithResultException.class})
public AlitaResult<?> handleChangeTokenException(WithResultException ex) {
AlitaResult<?> result = ex.getResult();
result.setRequestId(TracerUtils.getTraceId());
return result;
}
/**
* 处理校验异常
*
* @param ex
*/
@ExceptionHandler(value = {
MethodArgumentNotValidException.class,
BindException.class})
public AlitaResult<String> handleValidationException(Exception ex) {
BindingResult result;
if (ex instanceof MethodArgumentNotValidException) {
result = ((MethodArgumentNotValidException) ex).getBindingResult();
} else {
result = ((BindException) ex).getBindingResult();
}
if (!result.hasErrors()) {
return AlitaResult.error("操作失败");
}
StringJoiner joiner = new StringJoiner(",");
List<ObjectError> errors = result.getAllErrors();
errors.forEach(error -> {
if (error instanceof FieldError) {
joiner.add(((FieldError) error).getField() + "-" + error.getDefaultMessage());
} else {
joiner.add(error.getDefaultMessage());
}
});
String msg = String.format("数据异常:%s", joiner.toString());
LOGGER.error(msg);
AlitaResult<String> alitaResult = AlitaResult.error(AlitaResult.VALIDATION_ERROR_CODE, msg);
alitaResult.setRequestId(TracerUtils.getTraceId());
return alitaResult;
}
/**
* 处理未知的错误
*
* @param ex
*/
@ExceptionHandler(value = {ConstraintViolationException.class})
@Transactional(rollbackFor = RuntimeException.class)
public AlitaResult<String> handleConstraintViolationException(ConstraintViolationException ex) {
Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
StringJoiner joiner = new StringJoiner(CommonConstants.COMMA);
constraintViolations.stream().forEach(error -> joiner.add(error.getMessage()));
String msg = String.format("数据异常:%s", joiner.toString());
LOGGER.error(msg);
AlitaResult<String> alitaResult = AlitaResult.error(AlitaResult.VALIDATION_ERROR_CODE, msg);
alitaResult.setRequestId(TracerUtils.getTraceId());
return alitaResult;
}
/**
* 处理未知的错误
*
* @param ex
*/
@ExceptionHandler(value = {Exception.class})
@Transactional(rollbackFor = RuntimeException.class)
public AlitaResult<String> handleUnknownException(Exception ex) {
String msg = null;
if (ex instanceof HttpRequestMethodNotSupportedException) {
msg = "请求错误:" + ((HttpRequestMethodNotSupportedException) ex).getSupportedMethods()[0]
+ SpringContextHolder.getRequest().getRequestURI();
} else {
LOGGER.error("",ex);
ExceptionLogDTO exceptionLogDTO = ExceptionLogDTO.builder().traceId(TracerUtils.getTraceId())
.msg(ExceptionUtils.getStackTrace(ex)).build();
LogUtils.log(exceptionLogDTO);
msg = String.format("系统异常[%s#%s],请稍后重试.",
EnvironmentUtils.getSystemCode(), exceptionLogDTO.getErrNo()
);
LOGGER.error("系统异常,错误号:{}", exceptionLogDTO.getErrNo());
}
AlitaResult<String> alitaResult = AlitaResult.error(msg);
alitaResult.setRequestId(TracerUtils.getTraceId());
return alitaResult;
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(" \t\r\n\f", true));
}
}

View 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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}
}

View File

@@ -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();
}
}