错误处理
This commit is contained in:
1 parent
cf68147d08
commit
7202f0783a
3 files changed
+68
-9
No files matched your search
@@ -7,6 +7,7 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.client.RestClientResponseException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -54,9 +55,15 @@ public class ChainExecutorClient {
|
||||
throw new IllegalArgumentException("内网执行服务需配置内网 HTTP/HTTPS 根地址和至少32位服务凭据");
|
||||
}
|
||||
log.info("内网链上执行通道已启用,开始验证执行器服务,传输协议={}", target.getScheme().toUpperCase());
|
||||
requireResponse("/internal/v1/connectivity", "CONNECTED", "联通测试");
|
||||
requireResponse("/internal/v1/readiness", "READY", "业务就绪检查");
|
||||
log.info("内网链上执行器服务验证通过,联通状态=CONNECTED,业务状态=READY");
|
||||
try {
|
||||
requireResponse("/internal/v1/connectivity", "CONNECTED", "联通测试");
|
||||
requireResponse("/internal/v1/readiness", "READY", "业务就绪检查");
|
||||
log.info("内网链上执行器服务验证通过,联通状态=CONNECTED,业务状态=READY");
|
||||
} catch (IllegalStateException unavailable) {
|
||||
// 执行器可能与支付系统存在短暂启动顺序差异。保留支付管理能力,实际远程执行仍由ready()拒绝。
|
||||
log.error("内网链上执行器启动验证未通过,支付系统继续启动,远程链上操作暂不可用,诊断={}",
|
||||
unavailable.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEnabled() { return enabled; }
|
||||
@@ -67,7 +74,7 @@ public class ChainExecutorClient {
|
||||
return "READY".equals(http.exchange(url + "/internal/v1/readiness", HttpMethod.GET,
|
||||
new HttpEntity<Void>(headers()), String.class).getBody());
|
||||
} catch (Exception failure) {
|
||||
log.warn("内网链上执行器业务就绪检查失败,异常类型={}", failure.getClass().getSimpleName());
|
||||
log.warn("内网链上执行器业务就绪检查失败,诊断={}", diagnostic(failure));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -79,8 +86,7 @@ public class ChainExecutorClient {
|
||||
log.info("已通知内网链上执行器扫描远程任务");
|
||||
} catch (Exception failure) {
|
||||
// 数据库已保存任务;通知失败由执行器后台补扫,不能降级成另一笔密码交易。
|
||||
log.warn("通知内网链上执行器失败,任务已持久化并等待执行器补扫,异常类型={}",
|
||||
failure.getClass().getSimpleName());
|
||||
log.warn("通知内网链上执行器失败,任务已持久化并等待执行器补扫,诊断={}", diagnostic(failure));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,11 +98,22 @@ public class ChainExecutorClient {
|
||||
if (!expected.equals(body)) throw new IllegalStateException("响应内容不符合执行器协议");
|
||||
log.info("内网链上执行器{}通过", stage);
|
||||
} catch (Exception failure) {
|
||||
log.error("内网链上执行器{}失败,异常类型={}", stage, failure.getClass().getSimpleName());
|
||||
throw new IllegalStateException("内网链上执行器" + stage + "失败,请检查服务、TLS、白名单、令牌和钱包配置");
|
||||
String diagnostic = diagnostic(failure);
|
||||
log.error("内网链上执行器{}失败,诊断={}", stage, diagnostic);
|
||||
throw new IllegalStateException(stage + "失败[" + diagnostic + "]");
|
||||
}
|
||||
}
|
||||
|
||||
/** 仅保留异常类型或HTTP状态,避免URL、令牌及底层响应正文进入日志。 */
|
||||
private static String diagnostic(Exception failure) {
|
||||
if (failure instanceof RestClientResponseException) {
|
||||
return "HTTP_" + ((RestClientResponseException) failure).getRawStatusCode();
|
||||
}
|
||||
Throwable cause = failure;
|
||||
while (cause.getCause() != null && cause.getCause() != cause) cause = cause.getCause();
|
||||
return cause.getClass().getSimpleName();
|
||||
}
|
||||
|
||||
private HttpHeaders headers() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Authorization", "Bearer " + token);
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.ruoyi.system.chain;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/** 启动联通失败只降低远程执行能力,配置格式错误仍须阻止启动。 */
|
||||
public class ChainExecutorClientTest {
|
||||
@Test
|
||||
public void unavailableExecutorDoesNotPreventPaymentSystemStartup() throws Exception {
|
||||
ChainExecutorClient client = client("http://127.0.0.1:1");
|
||||
client.validate();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidExecutorUrlStillPreventsStartup() throws Exception {
|
||||
ChainExecutorClient client = client("http://example.com:9088");
|
||||
try {
|
||||
client.validate();
|
||||
fail("域名地址应在发起请求前被拒绝");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
// 只允许内网字面IP,避免DNS重绑定或误连公网。
|
||||
}
|
||||
}
|
||||
|
||||
private ChainExecutorClient client(String url) throws Exception {
|
||||
ChainExecutorClient client = new ChainExecutorClient();
|
||||
set(client, "enabled", true);
|
||||
set(client, "url", url);
|
||||
set(client, "token", "test-only-token-012345678901234567890");
|
||||
return client;
|
||||
}
|
||||
|
||||
private void set(Object target, String name, Object value) throws Exception {
|
||||
Field field = target.getClass().getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
field.set(target, value);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user