feat(exception): 增加 SQL 完整性约束异常处理- 新增 SQLIntegrityConstraintViolationException 异常处理方法
- 添加相应的错误码和错误信息- 优化 SQLSyntaxErrorException 异常处理,使用错误码代替直接返回错误信息
This commit is contained in:
@@ -4,6 +4,9 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.youlai.boot.common.result.Result;
|
import com.youlai.boot.common.result.Result;
|
||||||
import com.youlai.boot.common.result.ResultCode;
|
import com.youlai.boot.common.result.ResultCode;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.validation.ConstraintViolation;
|
||||||
|
import jakarta.validation.ConstraintViolationException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.TypeMismatchException;
|
import org.springframework.beans.TypeMismatchException;
|
||||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||||
@@ -21,10 +24,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|||||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||||
|
|
||||||
import jakarta.servlet.ServletException;
|
import java.sql.SQLIntegrityConstraintViolationException;
|
||||||
import jakarta.validation.ConstraintViolation;
|
|
||||||
import jakarta.validation.ConstraintViolationException;
|
|
||||||
|
|
||||||
import java.sql.SQLSyntaxErrorException;
|
import java.sql.SQLSyntaxErrorException;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@@ -207,7 +207,20 @@ public class GlobalExceptionHandler {
|
|||||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||||
public <T> Result<T> processSQLSyntaxErrorException(SQLSyntaxErrorException e) {
|
public <T> Result<T> processSQLSyntaxErrorException(SQLSyntaxErrorException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return Result.failed(e.getMessage());
|
return Result.failed(ResultCode.DATABASE_EXECUTION_SYNTAX_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理 SQL 违反了完整性约束
|
||||||
|
* <p>
|
||||||
|
* 当 SQL 违反了完整性约束时,会抛出 SQLIntegrityConstraintViolationException 异常。
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
|
||||||
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||||
|
public <T> Result<T> handleSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
return Result.failed(ResultCode.INTEGRITY_CONSTRAINT_VIOLATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -243,12 +243,14 @@ public enum ResultCode implements IResultCode, Serializable {
|
|||||||
|
|
||||||
TABLE_NOT_EXIST("C0311", "表不存在"),
|
TABLE_NOT_EXIST("C0311", "表不存在"),
|
||||||
COLUMN_NOT_EXIST("C0312", "列不存在"),
|
COLUMN_NOT_EXIST("C0312", "列不存在"),
|
||||||
|
DATABASE_EXECUTION_SYNTAX_ERROR("C0313", "数据库执行语法错误"),
|
||||||
|
|
||||||
MULTIPLE_SAME_NAME_COLUMNS_IN_MULTI_TABLE_ASSOCIATION("C0321", "多表关联中存在多个相同名称的列"),
|
MULTIPLE_SAME_NAME_COLUMNS_IN_MULTI_TABLE_ASSOCIATION("C0321", "多表关联中存在多个相同名称的列"),
|
||||||
|
|
||||||
DATABASE_DEADLOCK("C0331", "数据库死锁"),
|
DATABASE_DEADLOCK("C0331", "数据库死锁"),
|
||||||
|
|
||||||
PRIMARY_KEY_CONFLICT("C0341", "主键冲突"),
|
PRIMARY_KEY_CONFLICT("C0341", "主键冲突"),
|
||||||
|
INTEGRITY_CONSTRAINT_VIOLATION("C0342", "违反了完整性约束"),
|
||||||
|
|
||||||
DATABASE_ACCESS_DENIED("C0351", "演示环境已禁用数据库写入功能,请本地部署修改数据库链接或开启Mock模式进行体验"),
|
DATABASE_ACCESS_DENIED("C0351", "演示环境已禁用数据库写入功能,请本地部署修改数据库链接或开启Mock模式进行体验"),
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user