From c9da1b0da1178911e383ddcaebecd1e088fa6004 Mon Sep 17 00:00:00 2001 From: Fixiaobai <fixiaobai@163.com> Date: 星期二, 05 九月 2023 16:37:07 +0800 Subject: [PATCH] 修改bug --- framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java | 34 ++++++++++++++++++++++++++++++++++ 1 files changed, 34 insertions(+), 0 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 976777b..29af084 100644 --- a/framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java +++ b/framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java @@ -1,7 +1,9 @@ package com.yuanchu.limslaboratory.handler; +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; @@ -10,17 +12,27 @@ 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 @@ -51,6 +63,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] + "銆戞搷浣滃け璐ワ紒"); } @@ -191,10 +208,27 @@ 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) { e.printStackTrace(); return Result.fail("缃戠粶杩炴帴澶辫触锛岃閫�鍑哄悗鍐嶈瘯"); } + + @ExceptionHandler({AuthException.class}) + public Result<?> noAuth(AuthException e){ + return Result.fail(Integer.valueOf(e.getCode()),e.getMsg()); + } + + } -- Gitblit v1.9.3