From d2ce4553e18131b9a60d7be53c496f0cbce699ca Mon Sep 17 00:00:00 2001
From: Fixiaobai <fixiaobai@163.com>
Date: 星期五, 08 九月 2023 14:47:47 +0800
Subject: [PATCH] 修改bug
---
framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java | 121 ++++++++++++++++++++++++++++++++++-----
1 files changed, 104 insertions(+), 17 deletions(-)
diff --git a/framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java b/framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java
index 7e4449a..d449a4e 100644
--- a/framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java
+++ b/framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java
@@ -1,25 +1,51 @@
package com.yuanchu.limslaboratory.handler;
+import com.yuanchu.limslaboratory.exception.ApplicationException;
+import com.yuanchu.limslaboratory.exception.AuthException;
+import com.yuanchu.limslaboratory.exception.MyFileException;
+import com.yuanchu.limslaboratory.utils.MyUtil;
+import com.yuanchu.limslaboratory.utils.ServletUtils;
import com.yuanchu.limslaboratory.vo.Result;
import lombok.extern.slf4j.Slf4j;
+import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
+import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.dao.DataAccessException;
+import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.jdbc.BadSqlGrammarException;
+import org.springframework.validation.ObjectError;
+import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.multipart.MultipartException;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.net.BindException;
import java.sql.SQLException;
+import java.sql.SQLIntegrityConstraintViolationException;
+import java.util.List;
+import java.util.stream.Collectors;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
+
+ @Value("${spring.servlet.multipart.max-file-size}")
+ private String maxFileSize;
+
+ @Value("${spring.servlet.multipart.max-request-size}")
+ private String maxRequestSize;
+
@ExceptionHandler(SQLException.class)
public Result<?> handlerSQLException(SQLException e)
{
@@ -38,6 +64,11 @@
log.error(String.valueOf(e));
String message = e.getCause().getMessage();
String[] split = message.split("'");
+ String servletPath = ServletUtils.getRequest().getServletPath();
+ System.out.println(servletPath);
+ if("/user/add_new_personnel".equals(servletPath)){
+ return Result.fail("璐﹀彿閲嶅锛屾坊鍔犲け璐ワ紝璇锋牳瀵瑰悗鎿嶄綔銆�");
+ }
return Result.fail("閲嶅娣诲姞锛氥��" + split[1] + "銆戞搷浣滃け璐ワ紒");
}
@@ -50,115 +81,171 @@
/** 杩愯鏃跺紓甯� */
@ExceptionHandler(RuntimeException.class)
public Result<?> runtimeExceptionHandler(RuntimeException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("杩愯鏃跺紓甯�");
}
/** 绫诲瀷杞崲寮傚父 */
@ExceptionHandler(ClassCastException.class)
public Result<?> classCastExceptionHandler(ClassCastException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("绫诲瀷杞崲寮傚父");
}
/** 鏂囦欢鏈壘鍒板紓甯� */
@ExceptionHandler(FileNotFoundException.class)
public Result<?> FileNotFoundException(FileNotFoundException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("鏂囦欢鏈壘鍒板紓甯�");
}
/** 鏁板瓧鏍煎紡寮傚父 */
@ExceptionHandler(NumberFormatException.class)
public Result<?> NumberFormatException(NumberFormatException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("鏁板瓧鏍煎紡寮傚父");
}
+
/** 瀹夊叏寮傚父 */
@ExceptionHandler(SecurityException.class)
public Result<?> SecurityException(SecurityException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("瀹夊叏寮傚父");
}
/** 绫诲瀷涓嶅瓨鍦ㄥ紓甯� */
@ExceptionHandler(TypeNotPresentException.class)
public Result<?> TypeNotPresentException(TypeNotPresentException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("绫诲瀷涓嶅瓨鍦ㄥ紓甯�");
}
/** IO寮傚父 */
@ExceptionHandler(IOException.class)
public Result<?> iOExceptionHandler(IOException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("IO寮傚父");
}
/** 鏈煡鏂规硶寮傚父 */
@ExceptionHandler(NoSuchMethodException.class)
public Result<?> noSuchMethodExceptionHandler(NoSuchMethodException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("鏈煡鏂规硶寮傚父");
}
/** 鏁扮粍瓒婄晫寮傚父 */
@ExceptionHandler(IndexOutOfBoundsException.class)
public Result<?> indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("鏁扮粍瓒婄晫寮傚父");
}
+
/** sql璇硶閿欒寮傚父 */
@ExceptionHandler(BadSqlGrammarException.class)
public Result<?> BadSqlGrammarException(BadSqlGrammarException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("sql璇硶閿欒寮傚父");
}
/** 鏃犳硶娉ㄥ叆bean寮傚父 */
@ExceptionHandler(NoSuchBeanDefinitionException.class)
public Result<?> NoSuchBeanDefinitionException(NoSuchBeanDefinitionException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("鏃犳硶娉ㄥ叆bean");
}
/** Http娑堟伅涓嶅彲璇诲紓甯� */
@ExceptionHandler({HttpMessageNotReadableException.class})
public Result<?> requestNotReadable(HttpMessageNotReadableException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("Http娑堟伅涓嶅彲璇�");
}
/** 400閿欒 */
@ExceptionHandler({TypeMismatchException.class})
public Result<?> requestTypeMismatch(TypeMismatchException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("鏈嶅姟鍣ㄥ紓甯�");
}
/** 500閿欒 */
@ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
public Result<?> server500(RuntimeException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("鏈嶅姟鍣ㄥ紓甯�");
}
/** 鏍堟孩鍑� */
@ExceptionHandler({StackOverflowError.class})
public Result<?> requestStackOverflow(StackOverflowError e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("鏍堟孩鍑哄紓甯�");
}
/** 闄ゆ暟涓嶈兘涓�0 */
@ExceptionHandler({ArithmeticException.class})
public Result<?> arithmeticException(ArithmeticException e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("闄ゆ暟涓嶈兘涓�0寮傚父");
+ }
+
+ /**
+ *鏂囦欢杩囧ぇ鎶ラ敊鎻愮ず
+ */
+ @ExceptionHandler({MultipartException.class})
+ public Result<?> fileUploadExceptionHandler(MultipartException e) {
+ String msg;
+ Throwable rootCause = e.getRootCause();
+ if (rootCause instanceof FileSizeLimitExceededException) {
+ msg="涓婁紶鏂囦欢杩囧ぇ銆愬崟涓枃浠跺ぇ灏忎笉寰楄秴杩�" + maxFileSize + "銆�";
+ }else if(rootCause instanceof SizeLimitExceededException){
+ msg="涓婁紶鏂囦欢杩囧ぇ銆愭�讳笂浼犲ぇ灏忎笉寰楄秴杩�" + maxRequestSize + "銆�";
+ }else {
+ msg="鏂囦欢涓婁紶澶辫触銆愭湇鍔″櫒寮傚父銆�";
+ }
+ return Result.fail(msg);
+ }
+
+ /** 鏂囦欢鍚庣紑鍚嶄笉閫氳繃杩斿洖鎻愮ず */
+ @ExceptionHandler({MyFileException.class})
+ public Result<?> myFileException(Exception e) {
+ return Result.fail("鎶辨瓑涓嶆敮鎸併��" + e.getMessage() +"銆戝悗缂�鐨勬枃浠讹紒");
+ }
+
+ /**
+ * 鍙傛暟妫�楠�
+ */
+ @ExceptionHandler({MethodArgumentNotValidException.class})
+ public Result<?> methodArgumentNotValidException(MethodArgumentNotValidException e) {
+ List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
+ String message = allErrors.stream().map(s -> s.getDefaultMessage()).collect(Collectors.joining(";"));
+ return Result.fail(message);
}
/** 鍏朵粬閿欒 */
@ExceptionHandler({Exception.class})
public Result<?> exception(Exception e) {
- log.error(e.getMessage(), e.getCause());
+ e.printStackTrace();
return Result.fail("缃戠粶杩炴帴澶辫触锛岃閫�鍑哄悗鍐嶈瘯");
}
+
+ /**
+ * 鏉冮檺
+ * @param e
+ * @return
+ */
+ @ExceptionHandler({AuthException.class})
+ public Result<?> noAuth(AuthException e){
+ return Result.fail(Integer.valueOf(e.getCode()),e.getMsg());
+ }
+
+ /**
+ * 涓氬姟寮傚父
+ * @param e
+ * @return
+ */
+ @ExceptionHandler({ApplicationException.class })
+ public Result<?> applicationError(ApplicationException e){
+ return Result.fail(e.getMsg());
+ }
+
}
--
Gitblit v1.9.3