|
@@ -78,18 +78,32 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
return o;
|
|
|
}
|
|
|
|
|
|
- // -- 自定义异常输出结构 ----------------------------------------------------
|
|
|
- public static void printException(Exception e, Boolean isPrintStack) {
|
|
|
- log.error("========================================================================");
|
|
|
+ // 记录当前 访问URL、访问人IP
|
|
|
+ private static void printRequestInfo() {
|
|
|
// 获取请求的URL
|
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
if (attributes != null) {
|
|
|
HttpServletRequest request = attributes.getRequest();
|
|
|
if (request != null) {
|
|
|
+ String method = request.getMethod();
|
|
|
String url = request.getRequestURL().toString();
|
|
|
- log.error(url);
|
|
|
+ String ip = request.getRemoteAddr();
|
|
|
+ // 记录URL和IP地址
|
|
|
+ log.warn("[" + method + "] " + url + ", (" + ip + ")");
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ // -- 自定义异常输出结构 (警告) ----------------------------------------------------
|
|
|
+ private static void printWarnException(Exception e) {
|
|
|
+ printRequestInfo();
|
|
|
+ log.warn(e.getClass().getName() + ": " + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // -- 自定义异常输出结构 (错误) ----------------------------------------------------
|
|
|
+ private static void printErrorException(Exception e, Boolean isPrintStack) {
|
|
|
+ log.error("========================================================================");
|
|
|
+ printRequestInfo();
|
|
|
// 记录 当前异常类的class名称、异常消息
|
|
|
log.error(e.getMessage() + " (" + e.getClass().getName() + ")");
|
|
|
// 记录异常的详细信息
|
|
@@ -99,8 +113,8 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
}
|
|
|
log.error("========================================================================");
|
|
|
}
|
|
|
- public static void printException(Exception e) {
|
|
|
- printException(e, false);
|
|
|
+ private static void printErrorException(Exception e) {
|
|
|
+ printErrorException(e, false);
|
|
|
}
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
@@ -111,7 +125,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
public Result handleConstraintViolationException(ConstraintViolationException e) {
|
|
|
|
|
|
System.out.println("****** ConstraintViolationException.class: ******");
|
|
|
- printException(e);
|
|
|
+ printErrorException(e);
|
|
|
|
|
|
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
|
|
|
Iterator<ConstraintViolation<?>> iterator = constraintViolations.iterator();
|
|
@@ -134,7 +148,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
public Result handlerMethodArgumentException(MethodArgumentNotValidException e){
|
|
|
System.out.println("****** MethodArgumentNotValidException.class: ******");
|
|
|
- printException(e);
|
|
|
+ printWarnException(e);
|
|
|
return Result.error(
|
|
|
ResultEnum.PARAMETER_EXCEPTION.getCode(),
|
|
|
e.getBindingResult().getAllErrors().get(0).getDefaultMessage(),
|
|
@@ -145,7 +159,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(MissingServletRequestParameterException.class)
|
|
|
public Result handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
|
|
|
System.out.println("****** MissingServletRequestParameterException.class: ******");
|
|
|
- printException(e, true);
|
|
|
+ printErrorException(e, true);
|
|
|
return Result.error(
|
|
|
ResultEnum.PARAMETER_EXCEPTION.getCode(),
|
|
|
"缺少参数 " + e.getParameterName() + " 或类型不匹配 (" + e.getParameterType() + ")",
|
|
@@ -161,7 +175,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(NumberFormatException.class)
|
|
|
public Result handleNumberFormatException(NumberFormatException e) {
|
|
|
System.out.println("****** NumberFormatException.class: ******");
|
|
|
- printException(e, true);
|
|
|
+ printErrorException(e, true);
|
|
|
return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -172,7 +186,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(CustomException.class)
|
|
|
public Result handleCustomException(CustomException e) {
|
|
|
System.out.println("****** CustomException.class: ******");
|
|
|
- printException(e);
|
|
|
+ printWarnException(e);
|
|
|
return Result.error(e.getErrorCode() != null ? e.getErrorCode() : ResultEnum.PARAMETER_EXCEPTION.getCode(), e.getMessage(), e.getErrorObject());
|
|
|
}
|
|
|
/**
|
|
@@ -181,7 +195,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(CustomExceptionSimple.class)
|
|
|
public String handleCustomExceptionSimple(CustomExceptionSimple e) {
|
|
|
System.out.println("****** CustomExceptionSimple.class: ******");
|
|
|
- printException(e);
|
|
|
+ printErrorException(e);
|
|
|
return e.getMessage();
|
|
|
}
|
|
|
|
|
@@ -192,7 +206,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(TooManyResultsException.class)
|
|
|
public Result handleTooManyResultsException(TooManyResultsException e) {
|
|
|
System.out.println("****** TooManyResultsException.class: ******");
|
|
|
- printException(e, true);
|
|
|
+ printErrorException(e, true);
|
|
|
return Result.error(ResultEnum.SERVICE_EXCEPTION.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -202,7 +216,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(LoginException.class)
|
|
|
public Result handleLoginException(LoginException e) {
|
|
|
System.out.println("****** LoginException.class: ******");
|
|
|
- printException(e, true);
|
|
|
+ printErrorException(e, true);
|
|
|
return Result.error(ResultEnum.AUTH_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -212,7 +226,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(AccessDeniedException.class)
|
|
|
public Result handleAccessDeniedException(AccessDeniedException e) {
|
|
|
System.out.println("****** AccessDeniedException.class: ******");
|
|
|
- printException(e, true);
|
|
|
+ printErrorException(e, true);
|
|
|
return Result.error(ResultEnum.AUTH_ROLE_ERROR.getCode(), ResultEnum.AUTH_ROLE_ERROR.getMessage());
|
|
|
}
|
|
|
|
|
@@ -223,7 +237,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(NoHandlerFoundException.class)
|
|
|
public Result handleNoHandlerFoundException(NoHandlerFoundException e) {
|
|
|
System.out.println("****** NoHandlerFoundException.class: ******");
|
|
|
- printException(e, true);
|
|
|
+ printErrorException(e, true);
|
|
|
return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -233,7 +247,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(MyBatisSystemException.class)
|
|
|
public Result handleMyBatisSystemException(MyBatisSystemException e, BindingResult bindingResult) { // , BindingResult bindingResult
|
|
|
System.out.println("****** MyBatisSystemException.class: ******");
|
|
|
- printException(e, true);
|
|
|
+ printErrorException(e, true);
|
|
|
|
|
|
Throwable cause = e.getCause();
|
|
|
if (cause instanceof PersistenceException) {
|
|
@@ -258,7 +272,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(DuplicateKeyException.class)
|
|
|
public Result handleDuplicateKeyException(DuplicateKeyException e) {
|
|
|
System.out.println("****** DuplicateKeyException.class: ******");
|
|
|
- printException(e, true);
|
|
|
+ printErrorException(e, true);
|
|
|
DuplicateKeyExceptionHandler handler = new DuplicateKeyExceptionHandler();
|
|
|
return handler.handleDuplicateKeyException(e);
|
|
|
}
|
|
@@ -269,7 +283,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(RedisConnectionFailureException.class)
|
|
|
public Result handleRedisConnectionFailureException(Exception e) {
|
|
|
System.out.println("****** RedisConnectionFailureException.class: ******");
|
|
|
- printException(e, true);
|
|
|
+ printErrorException(e, true);
|
|
|
return Result.error(ResultEnum.REDIS_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -279,7 +293,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
|
|
public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
|
|
System.out.println("****** HttpMessageNotReadableException.class: ******");
|
|
|
- printException(e);
|
|
|
+ printWarnException(e);
|
|
|
return Result.error(ResultEnum.HTTP_BODY_EMPTY.getCode(), e.getMessage() != null ? e.getMessage() : ResultEnum.HTTP_BODY_EMPTY.getMessage());
|
|
|
}
|
|
|
|
|
@@ -289,18 +303,17 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
|
|
System.out.println("****** HttpRequestMethodNotSupportedException.class: ******");
|
|
|
- printException(e);
|
|
|
+ printWarnException(e);
|
|
|
return Result.error(ResultEnum.HTTP_METHOD_ERROR.getCode(), ResultEnum.HTTP_METHOD_ERROR.getMessage());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 常见异常:
|
|
|
- * - 请求方法 Post/Get 不支持
|
|
|
+ * 其他异常:
|
|
|
*/
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
public Result handleException(Exception e) { // , BindingResult bindingResult
|
|
|
System.out.println("****** Exception.class: ******");
|
|
|
- printException(e, true);
|
|
|
+ printErrorException(e, true);
|
|
|
return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -308,7 +321,7 @@ public class GlobalExceptionHandler implements ResponseBodyAdvice<Object> {
|
|
|
@ExceptionHandler(IllegalArgumentException.class)
|
|
|
public Result handleIllegalArgumentException(IllegalArgumentException e) {
|
|
|
System.out.println("****** IllegalArgumentException.class: ******");
|
|
|
- printException(e);
|
|
|
+ printErrorException(e);
|
|
|
return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), ResultEnum.PARAMETER_EXCEPTION.getMessage(), e.getMessage());
|
|
|
}
|
|
|
|