From d0ba0324430a0010ecf47e5cc0e4df609d586cfd Mon Sep 17 00:00:00 2001
From: fuliqi <fuliqi@qq.com>
Date: 星期三, 19 六月 2024 11:31:00 +0800
Subject: [PATCH] 会议学生端查询
---
src/main/java/com/ycl/jxkg/config/spring/exception/ExceptionHandle.java | 79 ++++++++++++++++++++++++++++++++++-----
1 files changed, 69 insertions(+), 10 deletions(-)
diff --git a/src/main/java/com/ycl/jxkg/config/spring/exception/ExceptionHandle.java b/src/main/java/com/ycl/jxkg/config/spring/exception/ExceptionHandle.java
index 57792ee..4f1977a 100644
--- a/src/main/java/com/ycl/jxkg/config/spring/exception/ExceptionHandle.java
+++ b/src/main/java/com/ycl/jxkg/config/spring/exception/ExceptionHandle.java
@@ -1,17 +1,24 @@
package com.ycl.jxkg.config.spring.exception;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.ycl.jxkg.base.Result;
import com.ycl.jxkg.base.SystemCode;
import com.ycl.jxkg.utils.ErrorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
+import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+import javax.servlet.http.HttpServletRequest;
+import java.util.List;
import java.util.stream.Collectors;
/**
@@ -20,9 +27,14 @@
* Copyright (C), 2020-2024, 姝︽眽鎬濈淮璺宠穬绉戞妧鏈夐檺鍏徃
* @date 2021/12/25 9:45
*/
-@ControllerAdvice
+@RestControllerAdvice
public class ExceptionHandle {
+
private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
+ /**
+ * 鍏徃椤圭洰鐨勫寘缁撴瀯锛岀敤浜庣缉鐭敊璇棩蹇楃殑闀垮害
+ */
+ private final static String COMPANY_PACKAGE = "com.ycl.jxkg.";
/**
* Handler rest response.
@@ -32,8 +44,9 @@
*/
@ExceptionHandler(Exception.class)
@ResponseBody
- public Result handler(Exception e) {
- logger.error(e.getMessage(), e);
+ public Result handler(Exception e, HttpServletRequest request) {
+ String errMsg = String.format("绯荤粺寮傚父-%s", e.getMessage());
+ this.printExceptionLocation(e, request, errMsg);
return new Result<>(SystemCode.InnerError.getCode(), SystemCode.InnerError.getMessage());
}
@@ -43,14 +56,30 @@
* @param e the e
* @return the rest response
*/
- @ExceptionHandler(MethodArgumentNotValidException.class)
+ @ExceptionHandler(RuntimeException.class)
@ResponseBody
- public Result handler(MethodArgumentNotValidException e) {
- String errorMsg = e.getBindingResult().getAllErrors().stream().map(file -> {
- FieldError fieldError = (FieldError) file;
- return ErrorUtil.parameterErrorFormat(fieldError.getField(), fieldError.getDefaultMessage());
- }).collect(Collectors.joining());
- return new Result<>(SystemCode.ParameterValidError.getCode(), errorMsg);
+ public Result handler(RuntimeException e, HttpServletRequest request) {
+ String errMsg = String.format("绯荤粺寮傚父-%s", e.getMessage());
+ this.printExceptionLocation(e, request, errMsg);
+ return new Result<>(SystemCode.InnerError.getCode(), e.getMessage());
+ }
+
+ /**
+ * JSON浼犲弬鏁版嵁鏍¢獙寮傚父
+ *
+ * @param e
+ * @param request
+ * @return
+ * @throws JsonProcessingException
+ */
+ @ExceptionHandler(MethodArgumentNotValidException.class)
+ public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e,
+ HttpServletRequest request) throws JsonProcessingException {
+ List<String> err = e.getBindingResult().getAllErrors().stream().map(error -> error.getDefaultMessage()).collect(Collectors.toList());
+ String s = new ObjectMapper().writeValueAsString(err);
+ String errMsg = String.format("鍙傛暟鏍¢獙澶辫触-%s", s);
+ this.printExceptionLocation(e, request, errMsg);
+ return Result.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), s);
}
/**
@@ -69,5 +98,35 @@
return new Result<>(SystemCode.ParameterValidError.getCode(), errorMsg);
}
+ /**
+ * 璇锋眰鏂瑰紡涓嶆敮鎸佸紓甯�
+ *
+ * @param e
+ * @param request
+ * @return
+ */
+ @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
+ public Result handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
+ HttpServletRequest request) {
+ String errMSg = String.format("涓嶆敮鎸�%s璇锋眰", e.getMethod());
+ this.printExceptionLocation(e, request, errMSg);
+ return Result.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
+ }
+ /**
+ * 鎵撳嵃寮傚父鍑虹幇浣嶇疆
+ *
+ * @param e
+ */
+ private void printExceptionLocation(Throwable e, HttpServletRequest request, String errMsg) {
+ StackTraceElement stackTraceElement = e.getStackTrace()[0];
+ String className = stackTraceElement.getClassName().contains(COMPANY_PACKAGE) ?
+ stackTraceElement.getClassName().split(COMPANY_PACKAGE)[1] : stackTraceElement.getClassName();
+ logger.error("鎺ュ彛锛氥�恵}銆�, 寮傚父绫伙細銆恵}銆戯紝鏂规硶锛氥�恵}銆戯紝鎵�鍦ㄨ锛氥�恵}銆�, 閿欒淇℃伅锛氥�恵}銆�",
+ request.getRequestURI(),
+ className,
+ stackTraceElement.getMethodName(),
+ stackTraceElement.getLineNumber(),
+ errMsg);
+ }
}
--
Gitblit v1.8.0