fix - 关于一些批量操作,对数据库的外键约束异常信息进行过滤

This commit is contained in:
wangli 2024-11-27 11:34:30 +08:00
parent 7f42c1c902
commit 9930f782ca

View File

@ -1,6 +1,7 @@
package cn.axzo.workflow.core.engine.cmd;
import cn.axzo.workflow.core.common.exception.WorkflowEngineException;
import com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException;
import org.apache.ibatis.exceptions.PersistenceException;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.FlowableOptimisticLockingException;
@ -10,7 +11,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Arrays;
import java.util.Objects;
/**
* CommandContext 中的 WorkflowEngineException 进行日志降级
@ -42,13 +45,24 @@ public class CustomCommandContext extends CommandContext {
LOGGER.info("Error while closing command context", exception);
} else if (exception instanceof WorkflowEngineException) {
LOGGER.warn("Workflow error while closing command context", exception);
} else if (exception instanceof PersistenceException &&
StringUtils.hasText(exception.getMessage()) &&
Arrays.stream(PERSISTENCE_EXCEPTION_WARN_MESSAGE).anyMatch(m -> exception.getMessage().contains(m))) {
LOGGER.warn("persistence error while closing command context", exception);
} else if (exception instanceof PersistenceException) {
Throwable rootCause = getRootCause(exception);
if (rootCause instanceof MySQLTransactionRollbackException) {
LOGGER.warn("MySQL transaction rollback exception : {}", rootCause.getMessage(), rootCause);
} if (rootCause instanceof SQLIntegrityConstraintViolationException){
LOGGER.warn("SQL constraint violation exception : {}", rootCause.getMessage(), rootCause);
} else {
LOGGER.warn("persistence error while closing command context:{}", exception.getMessage(), exception);
}
} else {
LOGGER.error("Error while closing command context", exception);
}
}
private Throwable getRootCause(Throwable throwable) {
while (Objects.nonNull(throwable.getCause())) {
throwable = throwable.getCause();
}
return throwable;
}
}