Fixiaobai
2023-09-07 e29f147aab5b0b0b794d611b522b67b94423e3cf
framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java
@@ -1,8 +1,10 @@
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;
@@ -11,7 +13,11 @@
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;
@@ -25,6 +31,7 @@
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;
@@ -57,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] + "】操作失败!");
    }
@@ -91,6 +103,7 @@
        e.printStackTrace();
        return Result.fail("数字格式异常");
    }
    /** 安全异常 */
    @ExceptionHandler(SecurityException.class)
    public Result<?> SecurityException(SecurityException e) {
@@ -125,6 +138,7 @@
        e.printStackTrace();
        return Result.fail("数组越界异常");
    }
    /** sql语法错误异常 */
    @ExceptionHandler(BadSqlGrammarException.class)
    public Result<?> BadSqlGrammarException(BadSqlGrammarException e) {
@@ -214,8 +228,24 @@
        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());
    }
}