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 | 64 +++++++++++++++++++++++++------
1 files changed, 51 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 0eb7f5e..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;
@@ -97,6 +98,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);
}
@@ -114,6 +119,10 @@
}
if (ex instanceof AccessDeniedException) {
return accessDeniedExceptionHandler(request, (AccessDeniedException) ex);
+ }
+ // 鍙傛暟鏍¢獙绫诲紓甯革紙濡� Assert.notNull 鎶涘嚭鐨� IllegalArgumentException锛夊睘浜庡鎴风闂锛屼笉搴旀寜 500 澶勭悊
+ if (ex instanceof IllegalArgumentException) {
+ return illegalArgumentExceptionHandler((IllegalArgumentException) ex);
}
return defaultExceptionHandler(request, ex);
}
@@ -166,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)
@@ -192,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)));
}
/**
@@ -265,6 +287,18 @@
public CommonResult<?> httpMediaTypeNotSupportedExceptionHandler(HttpMediaTypeNotSupportedException ex) {
log.warn("[httpMediaTypeNotSupportedExceptionHandler]", ex);
return CommonResult.error(BAD_REQUEST.getCode(), String.format("璇锋眰绫诲瀷涓嶆纭�:%s", ex.getMessage()));
+ }
+
+ /**
+ * 澶勭悊鍙傛暟闈炴硶寮傚父
+ *
+ * 鏉ユ簮鏄紝涓氬姟浠g爜涓娇鐢� {@link Assert#notNull} 绛夋柇瑷�鏍¢獙鍏ュ弬锛屾垨缂撳瓨娉ㄨВ浣跨敤绌� key銆�
+ * 杩欑被闂灞炰簬璋冪敤鏂逛紶鍙備笉姝g‘锛岃繑鍥� 400 鑰屼笉鏄� 500锛岄伩鍏嶅墠绔妸鍙傛暟闂璇垽涓虹郴缁熸晠闅溿��
+ */
+ @ExceptionHandler(value = IllegalArgumentException.class)
+ public CommonResult<?> illegalArgumentExceptionHandler(IllegalArgumentException ex) {
+ log.warn("[illegalArgumentExceptionHandler]", ex);
+ return CommonResult.error(BAD_REQUEST.getCode(), ex.getMessage());
}
/**
@@ -358,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