20260716215550

This commit is contained in:
oneao committed 2026-07-16 21:55:51 +08:00
1 parent 5cd4e1e1d2
commit 49dafe524c
21 files changed
+1218 -272

No files matched your search

@@ -28,6 +28,9 @@ public class OrgDataSourceFactory {
dataSource.setTestWhileIdle(poolProperties.isTestWhileIdle());
dataSource.setTestOnBorrow(poolProperties.isTestOnBorrow());
dataSource.setTestOnReturn(poolProperties.isTestOnReturn());
dataSource.setConnectionErrorRetryAttempts(0);
dataSource.setBreakAfterAcquireFailure(true);
dataSource.setFailFast(true);
dataSource.setTimeBetweenEvictionRunsMillis(
poolProperties.getTimeBetweenEvictionRuns().toMillis()
);
@@ -1,5 +1,6 @@
package cn.g3soft.fmsapi.database;
import cn.g3soft.fmsapi.exception.AuthenticationException;
import cn.g3soft.fmsapi.exception.BusinessException;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
@@ -39,7 +40,7 @@ public class OrgDatabaseConfigLoader {
throw new IllegalArgumentException("机构配置文件路径不合法");
}
if (!Files.isRegularFile(configFile)) {
throw new IllegalStateException("未找到机构数据库配置文件: " + configFile);
throw new AuthenticationException("机构码错误");
}
Properties properties = new Properties();
@@ -0,0 +1,8 @@
package cn.g3soft.fmsapi.exception;
public class AuthenticationException extends BusinessException {
public AuthenticationException(String message) {
super(message);
}
}
@@ -4,23 +4,48 @@ import cn.g3soft.fmsapi.utils.ApiResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLException;
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(AuthenticationException.class)
public ApiResponse<Void> handleAuthenticationException(AuthenticationException exception) {
return ApiResponse.fail(ApiResponse.AUTH_ERROR_CODE, exception.getMessage());
}
@ExceptionHandler(BusinessException.class)
public ApiResponse<Void> handleBusinessException(BusinessException exception) {
return ApiResponse.fail(exception.getMessage());
}
@ExceptionHandler({SQLException.class, DataAccessException.class})
public ApiResponse<Void> handleDatabaseException(
Exception exception,
HttpServletRequest request
) {
logSystemException(exception, request);
return systemErrorResponse();
}
@ExceptionHandler(Exception.class)
public ApiResponse<Void> handleException(
Exception exception,
HttpServletRequest request
) {
logSystemException(exception, request);
return systemErrorResponse();
}
private void logSystemException(
Exception exception,
HttpServletRequest request
) {
LOGGER.error(
"系统异常,method={},uri={}",
@@ -28,6 +53,9 @@ public class GlobalExceptionHandler {
request.getRequestURI(),
exception
);
}
private ApiResponse<Void> systemErrorResponse() {
return ApiResponse.fail(
ApiResponse.SYSTEM_ERROR_CODE,
"系统异常,请稍后重试"
@@ -17,10 +17,10 @@ public class DataService {
public List<Map<String, Object>> loadData(Map<String, Object> params) throws SQLException {
return dbUtils.loadData(
ParamUtils.getRequiredString(params, "viewName"),
ParamUtils.getString(params, "searchCondition"),
ParamUtils.getString(params, "orderField"),
ParamUtils.getList(params, "searchColumns")
ParamUtils.getRequiredString(params, "view_name"),
ParamUtils.getString(params, "search_condition"),
ParamUtils.getString(params, "order_field"),
ParamUtils.getList(params, "search_columns")
);
}
@@ -0,0 +1,30 @@
package cn.g3soft.fmsapi.database;
import com.alibaba.druid.pool.DruidDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
class OrgDataSourceFactoryTests {
@Test
void databaseConnectionFailureDoesNotRetry() {
OrgDatabaseProperties properties = new OrgDatabaseProperties();
OrgDataSourceFactory factory = new OrgDataSourceFactory();
ReflectionTestUtils.setField(factory, "properties", properties);
OrgDatabaseConfig config = new OrgDatabaseConfig(
"jdbc:sqlserver://127.0.0.1:1433;databaseName=test",
"test",
"test",
"com.microsoft.sqlserver.jdbc.SQLServerDriver"
);
try (DruidDataSource dataSource = factory.create("TEST", config)) {
assertThat(dataSource.getConnectionErrorRetryAttempts()).isZero();
assertThat(dataSource.isBreakAfterAcquireFailure()).isTrue();
assertThat(dataSource.isFailFast()).isTrue();
}
}
}
@@ -0,0 +1,33 @@
package cn.g3soft.fmsapi.database;
import cn.g3soft.fmsapi.exception.AuthenticationException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.test.util.ReflectionTestUtils;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class OrgDatabaseConfigLoaderTests {
@TempDir
Path configDir;
@Test
void missingConfigFileIsReportedAsInvalidOrgId() {
OrgDatabaseProperties properties = new OrgDatabaseProperties();
properties.setConfigDir(configDir);
OrgDatabaseConfigLoader loader = new OrgDatabaseConfigLoader();
ReflectionTestUtils.setField(loader, "properties", properties);
loader.init();
AuthenticationException exception = assertThrows(
AuthenticationException.class,
() -> loader.load("UNKNOWN")
);
assertEquals("机构码错误", exception.getMessage());
}
}
@@ -0,0 +1,27 @@
package cn.g3soft.fmsapi.exception;
import cn.g3soft.fmsapi.utils.ApiResponse;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import java.sql.SQLException;
import static org.assertj.core.api.Assertions.assertThat;
class GlobalExceptionHandlerTests {
@Test
void databaseExceptionReturnsSystemError() {
GlobalExceptionHandler handler = new GlobalExceptionHandler();
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/api/auth/login");
ApiResponse<Void> response = handler.handleDatabaseException(
new SQLException("connection failed"),
request
);
assertThat(response.code()).isEqualTo(ApiResponse.SYSTEM_ERROR_CODE);
assertThat(response.message()).isEqualTo("系统异常,请稍后重试");
assertThat(response.data()).isNull();
}
}