diff --git a/opsli-core/src/main/java/org/opsli/core/handler/GlobalExceptionHandler.java b/opsli-core/src/main/java/org/opsli/core/handler/GlobalExceptionHandler.java index 50ce0b3..138af06 100644 --- a/opsli-core/src/main/java/org/opsli/core/handler/GlobalExceptionHandler.java +++ b/opsli-core/src/main/java/org/opsli/core/handler/GlobalExceptionHandler.java @@ -4,12 +4,16 @@ import lombok.extern.slf4j.Slf4j; import org.opsli.common.api.ResultVo; import org.opsli.common.exception.EmptyException; import org.opsli.common.exception.ServiceException; +import org.opsli.core.msg.CoreMsg; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; + +import java.sql.SQLIntegrityConstraintViolationException; + import static org.opsli.common.constants.OrderConstants.EXCEPTION_HANDLER_ORDER; /** @@ -58,4 +62,19 @@ public class GlobalExceptionHandler { return errorR; } + /** + * 拦截 数据库主键冲突 + */ + @ExceptionHandler(SQLIntegrityConstraintViolationException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ResponseBody + public ResultVo sqlIntegrityConstraintViolationException(EmptyException e) { + log.error("数据异常:{}",e.getMessage(),e); + ResultVo errorR = ResultVo.error(CoreMsg.MySQL_EXCEPTION_SQL_INTEGRITY_CONSTRAINT_VIOLATION.getMessage()); + errorR.setCode(e.getCode()); + return errorR; + } + + + } \ No newline at end of file diff --git a/opsli-core/src/main/java/org/opsli/core/msg/CoreMsg.java b/opsli-core/src/main/java/org/opsli/core/msg/CoreMsg.java index df5818a..b838aa6 100644 --- a/opsli-core/src/main/java/org/opsli/core/msg/CoreMsg.java +++ b/opsli-core/src/main/java/org/opsli/core/msg/CoreMsg.java @@ -20,7 +20,12 @@ public enum CoreMsg implements BaseMsg { /** * Redis */ - REDIS_EXCEPTION_PUSH_SUB(10200,"Redis 订阅通道失败!") + REDIS_EXCEPTION_PUSH_SUB(10200,"Redis 订阅通道失败!"), + + /** + * MySQL + */ + MySQL_EXCEPTION_SQL_INTEGRITY_CONSTRAINT_VIOLATION(10300,"数据主键冲突或者已有该数据!"), ;