|
|
@@ -2,7 +2,9 @@ package com.haha.miniapp.config;
|
|
|
|
|
|
import cn.dev33.satoken.exception.NotLoginException;
|
|
|
import cn.dev33.satoken.exception.NotPermissionException;
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
import com.haha.common.exception.BusinessException;
|
|
|
+import com.haha.common.utils.TraceIdUtils;
|
|
|
import com.haha.common.vo.Result;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
@@ -15,6 +17,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
|
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -29,8 +32,13 @@ public class GlobalExceptionHandler {
|
|
|
* 处理业务异常
|
|
|
*/
|
|
|
@ExceptionHandler(BusinessException.class)
|
|
|
- public Result<Void> handleBusinessException(BusinessException e) {
|
|
|
- log.warn("业务异常: code={}, message={}", e.getCode(), e.getMessage());
|
|
|
+ public Result<Void> handleBusinessException(BusinessException e, HttpServletRequest request) {
|
|
|
+ String traceId = TraceIdUtils.getTraceId();
|
|
|
+ String requestPath = request.getRequestURI();
|
|
|
+
|
|
|
+ log.warn("[TraceId: {}] 业务异常 - 路径: {}, 异常代码: {}, 异常消息: {}",
|
|
|
+ traceId, requestPath, e.getCode(), e.getMessage());
|
|
|
+
|
|
|
return Result.error(e.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
|
@@ -39,8 +47,13 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(NotLoginException.class)
|
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
|
|
- public Result<Void> handleNotLoginException(NotLoginException e) {
|
|
|
- log.warn("未登录异常: {}", e.getMessage());
|
|
|
+ public Result<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
|
|
|
+ String traceId = TraceIdUtils.getTraceId();
|
|
|
+ String requestPath = request.getRequestURI();
|
|
|
+
|
|
|
+ log.warn("[TraceId: {}] 未登录异常 - 路径: {}, 异常类型: {}",
|
|
|
+ traceId, requestPath, e.getType());
|
|
|
+
|
|
|
String message;
|
|
|
if (e.getType().equals(NotLoginException.NOT_TOKEN)) {
|
|
|
message = "未提供token";
|
|
|
@@ -59,8 +72,13 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(NotPermissionException.class)
|
|
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
|
|
- public Result<Void> handleNotPermissionException(NotPermissionException e) {
|
|
|
- log.warn("无权限异常: {}", e.getMessage());
|
|
|
+ public Result<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
|
|
|
+ String traceId = TraceIdUtils.getTraceId();
|
|
|
+ String requestPath = request.getRequestURI();
|
|
|
+
|
|
|
+ log.warn("[TraceId: {}] 无权限异常 - 路径: {}, 需要权限: {}",
|
|
|
+ traceId, requestPath, e.getPermission());
|
|
|
+
|
|
|
return Result.error(403, "无访问权限");
|
|
|
}
|
|
|
|
|
|
@@ -69,11 +87,21 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
- public Result<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
|
|
+ public Result<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
|
|
|
+ String traceId = TraceIdUtils.getTraceId();
|
|
|
+ String requestPath = request.getRequestURI();
|
|
|
+
|
|
|
+ String fieldErrors = e.getBindingResult().getFieldErrors().stream()
|
|
|
+ .map(error -> String.format("%s: %s", error.getField(), error.getDefaultMessage()))
|
|
|
+ .collect(Collectors.joining("; "));
|
|
|
+
|
|
|
String message = e.getBindingResult().getFieldErrors().stream()
|
|
|
.map(FieldError::getDefaultMessage)
|
|
|
.collect(Collectors.joining(", "));
|
|
|
- log.warn("参数校验失败: {}", message);
|
|
|
+
|
|
|
+ log.warn("[TraceId: {}] 参数校验失败 - 路径: {}, 校验失败字段: {}",
|
|
|
+ traceId, requestPath, fieldErrors);
|
|
|
+
|
|
|
return Result.error(400, message);
|
|
|
}
|
|
|
|
|
|
@@ -82,11 +110,21 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(BindException.class)
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
- public Result<Void> handleBindException(BindException e) {
|
|
|
+ public Result<Void> handleBindException(BindException e, HttpServletRequest request) {
|
|
|
+ String traceId = TraceIdUtils.getTraceId();
|
|
|
+ String requestPath = request.getRequestURI();
|
|
|
+
|
|
|
+ String fieldErrors = e.getBindingResult().getFieldErrors().stream()
|
|
|
+ .map(error -> String.format("%s: %s", error.getField(), error.getDefaultMessage()))
|
|
|
+ .collect(Collectors.joining("; "));
|
|
|
+
|
|
|
String message = e.getBindingResult().getFieldErrors().stream()
|
|
|
.map(FieldError::getDefaultMessage)
|
|
|
.collect(Collectors.joining(", "));
|
|
|
- log.warn("参数绑定失败: {}", message);
|
|
|
+
|
|
|
+ log.warn("[TraceId: {}] 参数绑定失败 - 路径: {}, 绑定失败字段: {}",
|
|
|
+ traceId, requestPath, fieldErrors);
|
|
|
+
|
|
|
return Result.error(400, message);
|
|
|
}
|
|
|
|
|
|
@@ -95,8 +133,13 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(MissingServletRequestParameterException.class)
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
- public Result<Void> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
|
|
|
- log.warn("缺少必需参数: {}", e.getParameterName());
|
|
|
+ public Result<Void> handleMissingServletRequestParameterException(MissingServletRequestParameterException e, HttpServletRequest request) {
|
|
|
+ String traceId = TraceIdUtils.getTraceId();
|
|
|
+ String requestPath = request.getRequestURI();
|
|
|
+
|
|
|
+ log.warn("[TraceId: {}] 缺少必需参数 - 路径: {}, 参数名: {}, 参数类型: {}",
|
|
|
+ traceId, requestPath, e.getParameterName(), e.getParameterType());
|
|
|
+
|
|
|
return Result.error(400, "缺少必需参数: " + e.getParameterName());
|
|
|
}
|
|
|
|
|
|
@@ -105,8 +148,14 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
- public Result<Void> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
|
|
|
- log.warn("参数类型不匹配: {}={}", e.getName(), e.getValue());
|
|
|
+ public Result<Void> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) {
|
|
|
+ String traceId = TraceIdUtils.getTraceId();
|
|
|
+ String requestPath = request.getRequestURI();
|
|
|
+
|
|
|
+ log.warn("[TraceId: {}] 参数类型不匹配 - 路径: {}, 参数名: {}, 参数值: {}, 需要类型: {}",
|
|
|
+ traceId, requestPath, e.getName(), e.getValue(),
|
|
|
+ e.getRequiredType() != null ? e.getRequiredType().getSimpleName() : "未知");
|
|
|
+
|
|
|
return Result.error(400, "参数类型错误: " + e.getName());
|
|
|
}
|
|
|
|
|
|
@@ -115,8 +164,13 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(IllegalArgumentException.class)
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
- public Result<Void> handleIllegalArgumentException(IllegalArgumentException e) {
|
|
|
- log.warn("非法参数: {}", e.getMessage());
|
|
|
+ public Result<Void> handleIllegalArgumentException(IllegalArgumentException e, HttpServletRequest request) {
|
|
|
+ String traceId = TraceIdUtils.getTraceId();
|
|
|
+ String requestPath = request.getRequestURI();
|
|
|
+
|
|
|
+ log.warn("[TraceId: {}] 非法参数 - 路径: {}, 异常消息: {}",
|
|
|
+ traceId, requestPath, e.getMessage());
|
|
|
+
|
|
|
return Result.error(400, e.getMessage());
|
|
|
}
|
|
|
|
|
|
@@ -125,8 +179,23 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
- public Result<Void> handleException(Exception e) {
|
|
|
- log.error("系统异常: {}", e.getMessage(), e);
|
|
|
+ public Result<Void> handleException(Exception e, HttpServletRequest request) {
|
|
|
+ String traceId = TraceIdUtils.getTraceId();
|
|
|
+ String requestPath = request.getRequestURI();
|
|
|
+
|
|
|
+ // 尝试获取当前登录用户信息
|
|
|
+ String userInfo = "未登录";
|
|
|
+ try {
|
|
|
+ if (StpUtil.isLogin()) {
|
|
|
+ userInfo = "用户ID: " + StpUtil.getLoginIdAsString();
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ // 忽略获取用户信息时的异常
|
|
|
+ }
|
|
|
+
|
|
|
+ log.error("[TraceId: {}] 系统异常 - 路径: {}, 用户信息: {}, 异常消息: {}",
|
|
|
+ traceId, requestPath, userInfo, e.getMessage(), e);
|
|
|
+
|
|
|
return Result.error(500, "系统繁忙,请稍后重试");
|
|
|
}
|
|
|
}
|