Fixiaobai
2023-09-04 dd2554435b9ced61e2a6a06627145fca3bf2685b
framework/src/main/java/com/yuanchu/limslaboratory/handler/GlobalExceptionHandler.java
@@ -1,5 +1,6 @@
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.vo.Result;
@@ -10,17 +11,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
@@ -191,10 +202,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());
    }
}