From c0e7f848236e1ab5f945b98c32d12c81e3e9c991 Mon Sep 17 00:00:00 2001
From: Fixiaobai <fixiaobai@163.com>
Date: 星期四, 24 八月 2023 18:04:09 +0800
Subject: [PATCH] 角色管理
---
framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java | 85 ++++++++++++++++++++++++++++++++++--------
1 files changed, 68 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..e94b324 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,43 @@
package com.yuanchu.limslaboratory.handler;
+import com.yuanchu.limslaboratory.exception.MyFileException;
+import com.yuanchu.limslaboratory.utils.MyUtil;
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.DuplicateKeyException;
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.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)
{
@@ -50,115 +68,148 @@
/** 杩愯鏃跺紓甯� */
@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("缃戠粶杩炴帴澶辫触锛岃閫�鍑哄悗鍐嶈瘯");
}
}
--
Gitblit v1.9.3