fix(文案出口): 用户可见提示语一律走 UserMessages 把关,并补结果作业租约围栏
两件事同源:2026-10-09 任务 30456 的卡片上同时出现中文进度和一句英文异常原文。 围栏:派发链在执行前重读 job 行,让 activateRunningClaim 的 CAS 恒真,撞锁方 deferRunning 把在跑的行翻回 PENDING,markSuccess 静默写 0 行——成功被判成失败。 claim 快照现在贯穿到 runClaimed 不再回读,markSuccess 返回布尔并补 stage=SUCCESS_LOST 日志,让这次丢失可观测。 文案:普查数出 61 处把 ex.getMessage() 拼进用户可见字段(异常响应、任务 error_message、 结果作业 error_msg、看板 message),逐条翻译追不上新增速度,改为出口把关—— UserMessages.with(中文主干, 异常) 只拼接中文详情、orFallback(原文, 兜底句) 只放行含中文的串, 被换掉的原文进日志;GlobalExceptionHandler 按码分档兜底(401/4011/40902 各说各的话)。 另把 chunk-read-failed[N] 摘要写成"第 N 分片读取失败"、TASK_LOCK_BUSY 换成中文(40902 码不动, 客户端按码分支)。数字人两处与结果作业 worker 故意仍存原文:孤儿判定与三条 preserves raw 用例按 error_message 读它,界面侧由前端 userNotice 兜住,已在新尺子里点名豁免并反查豁免是否还被用到。 尺子:RawExceptionMessageSinkGuardTest(出口行数下限硬钉 1500+内存造违例自证)、UserMessagesTest、 UserFacingMessageChineseGuardTest 豁免表收到只剩 JSON 键 msg 一条。mvn -o test 4205 例 0 失败。
This commit is contained in:
+24
-7
@@ -3,6 +3,7 @@ package com.nanri.aiimage.common.exception;
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
import com.nanri.aiimage.common.service.TaskOwnerForwardService;
|
||||
import com.nanri.aiimage.common.util.UserMessages;
|
||||
import com.nanri.aiimage.config.TaskOperationLockConfig;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
@@ -54,9 +55,11 @@ public class GlobalExceptionHandler {
|
||||
.headers(safeHeaders)
|
||||
.body(response.getBody());
|
||||
} catch (BusinessException forwardEx) {
|
||||
String reason = UserMessages.orFallback(
|
||||
forwardEx.getMessage(), "任务归属实例暂时联系不上,请稍后重试");
|
||||
return forwardEx.getCode() == null
|
||||
? ApiResponse.fail(forwardEx.getMessage())
|
||||
: ApiResponse.fail(forwardEx.getCode(), forwardEx.getMessage());
|
||||
? ApiResponse.fail(reason)
|
||||
: ApiResponse.fail(forwardEx.getCode(), reason);
|
||||
} catch (Exception forwardEx) {
|
||||
// 转发失败是**瞬时基础设施故障**(归属实例正在滚动重启),不是业务结论,
|
||||
// 更不能表达成「任务不存活」。原先返回 ApiResponse.fail(40903) —— HTTP 200
|
||||
@@ -100,11 +103,13 @@ public class GlobalExceptionHandler {
|
||||
}
|
||||
if (Integer.valueOf(BusinessCodes.TASK_BUSY).equals(ex.getCode())) {
|
||||
// 锁竞争:必须如实返回失败 + 可重试码,否则 worker 会把「未落库」当成功而停止重试
|
||||
return ApiResponse.fail(BusinessCodes.TASK_BUSY, ex.getMessage());
|
||||
return ApiResponse.fail(BusinessCodes.TASK_BUSY,
|
||||
UserMessages.orFallback(ex.getMessage(), "这个任务正在处理中,请稍后重试"));
|
||||
}
|
||||
String userMessage = UserMessages.orFallback(ex.getMessage(), chineseFallbackFor(ex.getCode()));
|
||||
return ex.getCode() == null
|
||||
? ApiResponse.fail(ex.getMessage())
|
||||
: ApiResponse.fail(ex.getCode(), ex.getMessage());
|
||||
? ApiResponse.fail(userMessage)
|
||||
: ApiResponse.fail(ex.getCode(), userMessage);
|
||||
}
|
||||
|
||||
@ExceptionHandler(NoResourceFoundException.class)
|
||||
@@ -122,6 +127,17 @@ public class GlobalExceptionHandler {
|
||||
|| Integer.valueOf(DeviceSessionPolicy.CODE_KICKED).equals(code);
|
||||
}
|
||||
|
||||
/** 兜底句按码分档:整条消息被换成中文时,鉴权类失败不能说得像个业务错。 */
|
||||
private String chineseFallbackFor(Integer code) {
|
||||
if (Integer.valueOf(DeviceSessionPolicy.CODE_KICKED).equals(code)) {
|
||||
return "该账号已在其他设备登录,当前会话已下线";
|
||||
}
|
||||
if (Integer.valueOf(401).equals(code)) {
|
||||
return "登录状态已失效,请重新登录";
|
||||
}
|
||||
return "操作未能完成,请稍后重试";
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ApiResponse<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) { String message = ex.getBindingResult().getFieldError() != null
|
||||
? ex.getBindingResult().getFieldError().getDefaultMessage()
|
||||
@@ -131,7 +147,7 @@ public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
public ApiResponse<Void> handleConstraintViolationException(ConstraintViolationException ex) {
|
||||
return ApiResponse.fail(ex.getMessage());
|
||||
return ApiResponse.fail(UserMessages.orFallback(ex.getMessage(), "填写的内容不符合要求,请检查后重试"));
|
||||
}
|
||||
|
||||
@ExceptionHandler(AsyncRequestNotUsableException.class)
|
||||
@@ -142,7 +158,8 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(ResponseStatusException.class)
|
||||
public ResponseEntity<ApiResponse<Void>> handleResponseStatusException(ResponseStatusException ex) {
|
||||
String message = ex.getReason() == null || ex.getReason().isBlank() ? ex.getMessage() : ex.getReason();
|
||||
return ResponseEntity.status(ex.getStatusCode()).body(ApiResponse.fail(message));
|
||||
return ResponseEntity.status(ex.getStatusCode())
|
||||
.body(ApiResponse.fail(UserMessages.orFallback(message, "请求未能完成,请稍后重试")));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package com.nanri.aiimage.common.service;
|
||||
|
||||
import com.nanri.aiimage.common.util.UserMessages;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.exception.TaskOwnerMismatchException;
|
||||
import com.nanri.aiimage.config.InstanceRoutingProperties;
|
||||
@@ -173,7 +174,7 @@ public class TaskOwnerForwardService {
|
||||
try {
|
||||
return StreamUtils.copyToByteArray(request.getInputStream());
|
||||
} catch (IOException ex) {
|
||||
throw new BusinessException("读取转发请求体失败:" + ex.getMessage());
|
||||
throw new BusinessException(UserMessages.with("读取转发请求体失败", ex));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.nanri.aiimage.common.util;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 用户可见文案把关(2026-10-09 收口)。
|
||||
*
|
||||
* <p>背景:任务 30456 的结果文件其实已经生成、能下载,卡片上却跟着一条
|
||||
* {@code Similar ASIN result rows are empty; please retry result generation}。文案是英文不是
|
||||
* 最要命的——最要命的是它是**底层异常原文**:全仓有 61 处把 {@code ex.getMessage()} 直接
|
||||
* 拼进用户可见字段(异常响应、任务 error_message、结果作业 error_msg、看板 message),
|
||||
* 底层抛什么用户就看什么。逐条翻译译不完,因为新的 catch 天天在加。
|
||||
*
|
||||
* <p>本类把"只把中文给用户看"做成出口处的代码约束:中文照原样过,非中文换成中文句子,
|
||||
* 原文只进日志。诊断信息不丢——调用点本来就带着 {@code log.warn} 的堆栈。
|
||||
*/
|
||||
public final class UserMessages {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(UserMessages.class);
|
||||
|
||||
private UserMessages() {
|
||||
}
|
||||
|
||||
/** 中文主干 + 异常详情:详情是中文才拼接,英文详情丢弃(原文进 debug 日志)。 */
|
||||
public static String with(String headline, Throwable ex) {
|
||||
return with(headline, ex == null ? null : ex.getMessage());
|
||||
}
|
||||
|
||||
/** 中文主干 + 详情:详情是中文才拼接。 */
|
||||
public static String with(String headline, String detail) {
|
||||
String head = headline == null ? "" : headline.trim().replaceAll("[::]\\s*$", "");
|
||||
if (detail == null || detail.isBlank()) {
|
||||
return head;
|
||||
}
|
||||
if (hasChinese(detail)) {
|
||||
return head + ":" + detail.trim();
|
||||
}
|
||||
log.debug("[user-message] dropped non-chinese detail headline={} detail={}", head, detail);
|
||||
return head;
|
||||
}
|
||||
|
||||
/**
|
||||
* 整句把关:原文含中文则照原样返回,否则返回中文兜底句。
|
||||
*
|
||||
* <p>用于"整条消息都来自底层"的出口({@code ApiResponse.fail(ex.getMessage())}、
|
||||
* {@code setErrorMessage(ex.getMessage())})。被换掉时原文进 WARN,因为这类出口
|
||||
* 调用点未必另外记日志,静默替换会把根因从系统里彻底抹掉。
|
||||
*/
|
||||
public static String orFallback(String raw, String fallback) {
|
||||
if (raw != null && !raw.isBlank() && hasChinese(raw)) {
|
||||
return raw.trim();
|
||||
}
|
||||
log.warn("[user-message] replaced non-chinese message fallback={} raw={}",
|
||||
fallback, raw == null ? "" : raw);
|
||||
return fallback;
|
||||
}
|
||||
|
||||
/** 是否含汉字:含则认为是给人看的中文文案,可以原样透出。 */
|
||||
public static boolean hasChinese(String text) {
|
||||
if (text == null) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
char c = text.charAt(i);
|
||||
if (c >= '一' && c <= '鿿') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user