feat: 添加异常日志记录
This commit is contained in:
parent
2b05a1f1af
commit
66f71e5641
@ -2,17 +2,23 @@ package github.benjamin.equipreservebackend.exception;
|
|||||||
|
|
||||||
import github.benjamin.equipreservebackend.response.ResponseCode;
|
import github.benjamin.equipreservebackend.response.ResponseCode;
|
||||||
import github.benjamin.equipreservebackend.response.ResponseResult;
|
import github.benjamin.equipreservebackend.response.ResponseResult;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.security.authorization.AuthorizationDeniedException;
|
import org.springframework.security.authorization.AuthorizationDeniedException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
@ExceptionHandler(ApiException.class)
|
@ExceptionHandler(ApiException.class)
|
||||||
public ResponseResult<?> handleApiException(ApiException e) {
|
public ResponseResult<?> handleApiException(ApiException e) {
|
||||||
|
log.error("[API异常] 错误代码: {}, 错误信息: {}", e.getCode(), e.getMessage(), e);
|
||||||
return ResponseResult.fail(e.getCode(), e.getMessage());
|
return ResponseResult.fail(e.getCode(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,10 +28,20 @@ public class GlobalExceptionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public ResponseResult<?> handleException(Exception e) {
|
public ResponseResult<?> handleException(Exception e, HttpServletRequest request) {
|
||||||
// TODO 日志
|
log.error("[系统异常] 请求地址: {}, 方法: {}, 异常信息: {}, 请求参数: {}",
|
||||||
e.printStackTrace();
|
request.getRequestURI(),
|
||||||
|
request.getMethod(),
|
||||||
|
e.getMessage(),
|
||||||
|
getRequestParams(request),
|
||||||
|
e);
|
||||||
return ResponseResult.fail(ResponseCode.UNKNOWN_ERROR,"服务器内部异常,请联系开发人员");
|
return ResponseResult.fail(ResponseCode.UNKNOWN_ERROR,"服务器内部异常,请联系开发人员");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getRequestParams(HttpServletRequest request) {
|
||||||
|
Map<String, String[]> paramMap = request.getParameterMap();
|
||||||
|
return paramMap.entrySet().stream()
|
||||||
|
.map(entry -> entry.getKey() + "=" + Arrays.toString(entry.getValue()))
|
||||||
|
.collect(Collectors.joining(", ", "{", "}"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user