From bc00242ee0410684eb9f4c5fe0aa486dfebb3162 Mon Sep 17 00:00:00 2001
From: lead <lead@local>
Date: 星期二, 03 十一月 2026 10:09:31 +0800
Subject: [PATCH] fix(api): 修复全量接口扫描发现的 500 —— 由 70 个降到 0 个

---
 yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java |   48 +++++++++++++++++++++++++++++++++++-------------
 1 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java
index d8f1054..1c151e5 100644
--- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java
+++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java
@@ -36,6 +36,7 @@
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
 import org.springframework.web.multipart.MaxUploadSizeExceededException;
+import org.springframework.web.multipart.MultipartException;
 import org.springframework.web.servlet.NoHandlerFoundException;
 import org.springframework.web.servlet.resource.NoResourceFoundException;
 import tools.jackson.databind.exc.InvalidFormatException;
@@ -96,6 +97,10 @@
         }
         if (ex instanceof MaxUploadSizeExceededException) {
             return maxUploadSizeExceededExceptionHandler((MaxUploadSizeExceededException) ex);
+        }
+        // 涓婁紶绫绘帴鍙f敹鍒伴潪 multipart 璇锋眰锛堝墠绔紡浼犳枃浠� / 鐢� JSON 璋冪敤浜嗗鍏ユ帴鍙o級灞炰簬瀹㈡埛绔棶棰�
+        if (ex instanceof MultipartException) {
+            return multipartExceptionHandler((MultipartException) ex);
         }
         if (ex instanceof NoHandlerFoundException) {
             return noHandlerFoundExceptionHandler((NoHandlerFoundException) ex);
@@ -170,6 +175,17 @@
     }
 
     /**
+     * 澶勭悊鏂囦欢涓婁紶鎺ュ彛鏀跺埌闈� multipart 璇锋眰
+     *
+     * 渚嬪璇达紝Excel 瀵煎叆鎺ュ彛婕忎紶 file 瀛楁銆佹垨鍓嶇鐢� application/json 璋冪敤浜嗗鍏ユ帴鍙�
+     */
+    @ExceptionHandler(MultipartException.class)
+    public CommonResult<?> multipartExceptionHandler(MultipartException ex) {
+        log.warn("[multipartExceptionHandler]", ex);
+        return CommonResult.error(BAD_REQUEST.getCode(), "璇锋眰鍙傛暟涓嶆纭�:璇蜂娇鐢� multipart/form-data 涓婁紶鏂囦欢");
+    }
+
+    /**
      * 澶勭悊 SpringMVC 鍙傛暟缁戝畾涓嶆纭紝鏈川涓婁篃鏄�氳繃 Validator 鏍¢獙
      */
     @ExceptionHandler(BindException.class)
@@ -196,7 +212,9 @@
         if (StrUtil.startWith(ex.getMessage(), "Required request body is missing")) {
             return CommonResult.error(BAD_REQUEST.getCode(), "璇锋眰鍙傛暟绫诲瀷閿欒: request body 缂哄け");
         }
-        return defaultExceptionHandler(ServletUtils.getRequest(), ex);
+        // 鍏跺畠鍙嶅簭鍒楀寲澶辫触锛堝鎶婂璞′紶鎴愭暟缁勩�佸瓧娈电被鍨嬩笉鍖归厤锛夊悓鏍锋槸瀹㈡埛绔姤鏂囬棶棰橈紝涓嶅簲鎸� 500 澶勭悊
+        return CommonResult.error(BAD_REQUEST.getCode(),
+                String.format("璇锋眰鍙傛暟绫诲瀷閿欒:%s", StrUtil.maxLength(ex.getMessage(), 120)));
     }
 
     /**
@@ -374,27 +392,31 @@
         errorLog.setUserType(WebFrameworkUtils.getLoginUserType(request));
         // 璁剧疆寮傚父瀛楁
         errorLog.setExceptionName(e.getClass().getName());
-        errorLog.setExceptionMessage(ExceptionUtil.getMessage(e));
-        errorLog.setExceptionRootCauseMessage(ExceptionUtil.getRootCauseMessage(e));
-        errorLog.setExceptionStackTrace(ExceptionUtil.stacktraceToString(e));
+        // infra_api_error_log 鐨勮繖浜涘垪閮芥槸 NOT NULL锛涜�� MyBatis-Plus 鎻掑叆鏃朵細璺宠繃 null 瀛楁锛�
+        // 浜庢槸浠讳竴瀛楁涓� null 閮戒細鍙樻垚 "Field 'xxx' doesn't have a default value"锛屽紓甯告棩蹇楀弽鑰岃涓嶄笅鏉�
+        errorLog.setExceptionMessage(StrUtil.nullToEmpty(ExceptionUtil.getMessage(e)));
+        errorLog.setExceptionRootCauseMessage(StrUtil.nullToEmpty(ExceptionUtil.getRootCauseMessage(e)));
+        errorLog.setExceptionStackTrace(StrUtil.nullToEmpty(ExceptionUtil.stacktraceToString(e)));
         StackTraceElement[] stackTraceElements = e.getStackTrace();
         Assert.notEmpty(stackTraceElements, "寮傚父 stackTraceElements 涓嶈兘涓虹┖");
         StackTraceElement stackTraceElement = stackTraceElements[0];
         errorLog.setExceptionClassName(stackTraceElement.getClassName());
-        errorLog.setExceptionFileName(stackTraceElement.getFileName());
-        errorLog.setExceptionMethodName(stackTraceElement.getMethodName());
+        // 鏍堥《鍏冪礌鍙兘鏄� native 鏂规硶鎴� Lambda锛実etFileName() 涓� null锛�
+        // infra_api_error_log 鐨勮繖浜涘垪鏄� NOT NULL锛屼负 null 浼氬鑷村紓甯告棩蹇楁彃鍏ュけ璐ワ紙鏃ュ織閲屽彧鍓╀竴鏉� SQL 鎶ラ敊锛�
+        errorLog.setExceptionFileName(StrUtil.nullToEmpty(stackTraceElement.getFileName()));
+        errorLog.setExceptionMethodName(StrUtil.nullToEmpty(stackTraceElement.getMethodName()));
         errorLog.setExceptionLineNumber(stackTraceElement.getLineNumber());
         // 璁剧疆鍏跺畠瀛楁
-        errorLog.setTraceId(TracerUtils.getTraceId());
-        errorLog.setApplicationName(applicationName);
-        errorLog.setRequestUrl(request.getRequestURI());
+        errorLog.setTraceId(StrUtil.nullToEmpty(TracerUtils.getTraceId()));
+        errorLog.setApplicationName(StrUtil.nullToEmpty(applicationName));
+        errorLog.setRequestUrl(StrUtil.nullToEmpty(request.getRequestURI()));
         Map<String, Object> requestParams = MapUtil.<String, Object>builder()
                 .put("query", ServletUtils.getParamMap(request))
                 .put("body", ServletUtils.getBody(request)).build();
-        errorLog.setRequestParams(JsonUtils.toJsonString(requestParams));
-        errorLog.setRequestMethod(request.getMethod());
-        errorLog.setUserAgent(ServletUtils.getUserAgent(request));
-        errorLog.setUserIp(ServletUtils.getClientIP(request));
+        errorLog.setRequestParams(StrUtil.nullToEmpty(JsonUtils.toJsonString(requestParams)));
+        errorLog.setRequestMethod(StrUtil.nullToEmpty(request.getMethod()));
+        errorLog.setUserAgent(StrUtil.nullToEmpty(ServletUtils.getUserAgent(request)));
+        errorLog.setUserIp(StrUtil.nullToEmpty(ServletUtils.getClientIP(request)));
         errorLog.setExceptionTime(LocalDateTime.now());
     }
 

--
Gitblit v1.9.3