Przeglądaj źródła

fix(config): 全局异常处理器捕获 ClientAbortException,避免连接中断时二次抛异常

Xiaogang Liao 3 tygodni temu
rodzic
commit
d76a6b2a20

+ 9 - 0
cfc-backend/src/main/java/com/etotem/cfc/config/GlobalExceptionHandler.java

@@ -1,6 +1,7 @@
 package com.etotem.cfc.config;
 
 import com.etotem.cfc.common.Result;
+import java.io.IOException;
 import javax.servlet.http.HttpServletRequest;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -39,6 +40,14 @@ public class GlobalExceptionHandler {
         return Result.error(400, "请求参数格式错误");
     }
 
+    // 客户端中断连接(Broken pipe / ClientAbortException):连接已断开,无法写响应,
+    // 只记录日志,避免在 @ExceptionHandler 中二次抛异常导致 "Failure in @ExceptionHandler"
+    @ExceptionHandler(IOException.class)
+    public Result<Void> handleClientAbort(IOException e, HttpServletRequest request) {
+        log.warn("客户端连接中断: uri={}, error={}", request.getRequestURI(), e.getMessage());
+        return null;
+    }
+
     @ExceptionHandler(Exception.class)
     public Result<Void> handleException(Exception e, HttpServletRequest request) {
         log.error("未捕获异常: {} - {} | uri={}", e.getClass().getName(), e.getMessage(), request.getRequestURI(), e);