写流 QA:gracefulFlowError 透出业务校验消息 + 内部错误记栈(dogfooding 修)
逐流写 dogfooding(76 ADD 流采样:~76% 校验健康/3 NPE/1 SQL 语法/3 脚本错)发现系统性 UX 缺陷: gracefulFlowError 把真实异常消息整体替换成泛化「数据查询失败(数据源可能不可用)」,致: - 写流用户缺必填项时只见「数据源不可用」而非「单号必填」(校验反馈被屏蔽) - NPE 仅 log ex.getMessage()(=null)不记栈 → 黑盒不可诊断 修:businessMessage 沿 cause 链取保真层业务异常(Validation/Privilege/SameCode,消息面向用户) 透出;其余内部错误(NPE/SQL/脚本)保留泛化提示但 log 完整栈(可诊断,不泄露内部)。 按 simpleName 匹配避免硬依赖。FlowControllerTest +1(27 全绿)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -139,14 +140,48 @@ public class FlowController {
|
||||
* 写流的事务回滚在引擎 @Transactional 边界已完成(异常已逸出引擎方法),此处仅兜响应,不影响回滚。
|
||||
*/
|
||||
private static ResponseEntity<Map<String, Object>> gracefulFlowError(String id, RuntimeException ex) {
|
||||
log.warn("[FLOW] 执行异常 id={}: {}", sanitize(id), ex.getMessage());
|
||||
String bizMsg = businessMessage(ex);
|
||||
String msg;
|
||||
if (bizMsg != null) {
|
||||
// 业务/校验异常:消息本就面向最终用户(如「单号必填」),透出——修复写流校验反馈被
|
||||
// 泛化「数据查询失败」整体屏蔽的缺陷(QA dogfooding 发现:用户缺必填项却只见「数据源不可用」)。
|
||||
log.warn("[FLOW] 业务校验拦截 id={}: {}", sanitize(id), bizMsg);
|
||||
msg = bizMsg;
|
||||
} else {
|
||||
// 内部错误(NPE/SQL/脚本等):消息可能含内部细节,不透出;但记完整栈以便诊断
|
||||
// (原仅记 ex.getMessage(),NPE 的 message=null → 黑盒不可排查)。
|
||||
log.warn("[FLOW] 执行异常 id={}(内部错误,栈如下)", sanitize(id), ex);
|
||||
msg = "数据查询失败(数据源可能不可用): " + sanitize(id);
|
||||
}
|
||||
Map<String, Object> err = new LinkedHashMap<>();
|
||||
err.put("status", 1);
|
||||
err.put("msg", "数据查询失败(数据源可能不可用): " + sanitize(id));
|
||||
err.put("msg", msg);
|
||||
err.put("data", new LinkedHashMap<>());
|
||||
return ResponseEntity.ok(err);
|
||||
}
|
||||
|
||||
/** 业务/校验异常类名(消息面向用户,可安全透出);其它(SQL/NPE/脚本)消息不透出以免泄露内部。 */
|
||||
private static final Set<String> BUSINESS_EX_NAMES = Set.of(
|
||||
"ValidationException", "PrivilegeException", "SameCodeException");
|
||||
|
||||
/**
|
||||
* 沿 cause 链找首个业务异常(保真层 com.dsasp.server.common.exception.*)的非空消息——
|
||||
* 这类消息是设计给最终用户看的(如「单号必填」/「无权限」/「编码重复」),可安全透出;
|
||||
* NPE/SQL/脚本等内部异常返回 null(由调用方兜泛化提示 + 记栈)。按 simpleName 匹配避免硬依赖。
|
||||
*/
|
||||
static String businessMessage(Throwable t) {
|
||||
int guard = 0;
|
||||
while (t != null && guard++ < 20) {
|
||||
String m = t.getMessage();
|
||||
if (BUSINESS_EX_NAMES.contains(t.getClass().getSimpleName())
|
||||
&& m != null && !m.isBlank()) {
|
||||
return m.trim();
|
||||
}
|
||||
t = t.getCause();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把 amis crud 的字面分页参数 {@code page}/{@code perPage} 翻译成合成搜索流引擎
|
||||
* ({@code POJOCRUDService.listSelect})认得的分页键 {@link POJOCRUDService#PAGE_START}/
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.eip4.platform.metadata.web;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -27,6 +28,9 @@ import com.dsasp.server.common.core.FlowDefinition;
|
||||
import com.dsasp.server.common.core.service.FlowExcuteService;
|
||||
import com.dsasp.server.common.dataDic.bean.GeneralPOJO;
|
||||
import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
||||
import com.dsasp.server.common.exception.PrivilegeException;
|
||||
import com.dsasp.server.common.exception.SameCodeException;
|
||||
import com.dsasp.server.common.exception.ValidationException;
|
||||
import com.dsasp.server.common.user.service.LoggedInUser;
|
||||
import com.dsasp.server.common.util.UserSession;
|
||||
import com.eip4.platform.metadata.flow.FlowNameResolver;
|
||||
@@ -628,6 +632,21 @@ class FlowControllerTest {
|
||||
org.mockito.Mockito.verify(engine).excute(any(), any(), eq(flowId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("dogfooding 修复:gracefulFlowError 业务异常消息透出、内部错误兜泛化(businessMessage)")
|
||||
void gracefulFlowError_业务异常透出_内部错误泛化() {
|
||||
// 业务/校验异常(及 cause 链)→ 消息透出(写流校验反馈不再被「数据查询失败」整体屏蔽)
|
||||
assertEquals("单号必填", FlowController.businessMessage(new ValidationException("单号必填")));
|
||||
assertEquals("无权限操作", FlowController.businessMessage(
|
||||
new RuntimeException("wrap", new PrivilegeException("无权限操作"))));
|
||||
assertEquals("编码重复", FlowController.businessMessage(new SameCodeException("编码重复")));
|
||||
// 内部错误(NPE/普通 RuntimeException/SQL/脚本)→ null(不透出内部细节,调用方兜泛化提示+记栈)
|
||||
assertNull(FlowController.businessMessage(new NullPointerException()));
|
||||
assertNull(FlowController.businessMessage(new RuntimeException("错误的脚本。")));
|
||||
assertNull(FlowController.businessMessage(new RuntimeException("bad SQL grammar [INSERT INTO SYS_X]")));
|
||||
assertNull(FlowController.businessMessage(new RuntimeException("x", new IllegalStateException("y"))));
|
||||
}
|
||||
|
||||
/** ADMIN 上下文桩(CURRENTUSER=ADMIN)。 */
|
||||
private static Properties adminCtx() {
|
||||
Properties p = new Properties();
|
||||
|
||||
Reference in New Issue
Block a user