浏览代码

异常中断 SSE 捕获优化

hubin 9 月之前
父节点
当前提交
6871045fc9
共有 1 个文件被更改,包括 13 次插入3 次删除
  1. 13 3
      src/main/java/com/aizuda/boot/config/BootException.java

+ 13 - 3
src/main/java/com/aizuda/boot/config/BootException.java

@@ -7,6 +7,8 @@ package com.aizuda.boot.config;
 
 import com.aizuda.bpm.engine.exception.FlowLongException;
 import com.aizuda.core.api.ApiResult;
+import jakarta.servlet.http.HttpServletRequest;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
@@ -18,6 +20,7 @@ import java.io.IOException;
 /**
  * 自定义异常统一提示
  */
+@Slf4j
 @RestControllerAdvice
 public class BootException {
 
@@ -31,11 +34,18 @@ public class BootException {
         return ApiResult.failed(e.getMessage());
     }
 
+    /**
+     * IO 异常处理
+     */
     @ExceptionHandler(IOException.class)
-    @ResponseStatus(HttpStatus.OK)
+    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
     @ResponseBody
-    public ApiResult<String> handleIOException(IOException e) {
+    public void handleIOException(IOException e, HttpServletRequest request) {
         // 捕获 IO 异常,例如 sse 连接断开
-        return ApiResult.failed(e.getMessage());
+        String uri = request.getRequestURI();
+        if (!uri.contains("/sse")) {
+            // 记录非 sse 请求的异常日志
+            log.error("请求地址 {} 连接中断 {}", uri, e.getMessage());
+        }
     }
 }