¶Ô±ÈÐÂÎļþ |
| | |
| | | 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) |
| | | { |
| | | log.error(e.getMessage().toLowerCase(), e); |
| | | return Result.fail("æ°æ®æä½å¤±è´¥ï¼è¯·è系管çå"); |
| | | } |
| | | |
| | | /** |
| | | * å¯ä¸å¼å¨æ°æ®åºä¸éå¤ |
| | | * Duplicate entry ' ' for key ' ' |
| | | * @param eï¼éå¤é®å¼å¸¸ |
| | | * @return 199 |
| | | */ |
| | | @ExceptionHandler |
| | | public Result<?> DuplicateKeyException(DuplicateKeyException e) { |
| | | log.error(String.valueOf(e)); |
| | | String message = e.getCause().getMessage(); |
| | | String[] split = message.split("'"); |
| | | return Result.fail("é夿·»å ï¼ã" + split[1] + "ãæä½å¤±è´¥ï¼"); |
| | | } |
| | | |
| | | @ExceptionHandler |
| | | public Result<?> NullPointerException(NullPointerException e){ |
| | | e.printStackTrace(); |
| | | return Result.fail("é¨ååæ°ä¸ºç©ºï¼è¯·æ£æ¥ï¼"); |
| | | } |
| | | |
| | | /** è¿è¡æ¶å¼å¸¸ */ |
| | | @ExceptionHandler(RuntimeException.class) |
| | | public Result<?> runtimeExceptionHandler(RuntimeException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("è¿è¡æ¶å¼å¸¸"); |
| | | } |
| | | |
| | | /** ç±»å转æ¢å¼å¸¸ */ |
| | | @ExceptionHandler(ClassCastException.class) |
| | | public Result<?> classCastExceptionHandler(ClassCastException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("ç±»å转æ¢å¼å¸¸"); |
| | | } |
| | | /** æä»¶æªæ¾å°å¼å¸¸ */ |
| | | @ExceptionHandler(FileNotFoundException.class) |
| | | public Result<?> FileNotFoundException(FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("æä»¶æªæ¾å°å¼å¸¸"); |
| | | } |
| | | /** æ°åæ ¼å¼å¼å¸¸ */ |
| | | @ExceptionHandler(NumberFormatException.class) |
| | | public Result<?> NumberFormatException(NumberFormatException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("æ°åæ ¼å¼å¼å¸¸"); |
| | | } |
| | | /** å®å
¨å¼å¸¸ */ |
| | | @ExceptionHandler(SecurityException.class) |
| | | public Result<?> SecurityException(SecurityException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("å®å
¨å¼å¸¸"); |
| | | } |
| | | |
| | | /** ç±»åä¸åå¨å¼å¸¸ */ |
| | | @ExceptionHandler(TypeNotPresentException.class) |
| | | public Result<?> TypeNotPresentException(TypeNotPresentException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("ç±»åä¸åå¨å¼å¸¸"); |
| | | } |
| | | |
| | | /** IOå¼å¸¸ */ |
| | | @ExceptionHandler(IOException.class) |
| | | public Result<?> iOExceptionHandler(IOException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("IOå¼å¸¸"); |
| | | } |
| | | |
| | | /** æªç¥æ¹æ³å¼å¸¸ */ |
| | | @ExceptionHandler(NoSuchMethodException.class) |
| | | public Result<?> noSuchMethodExceptionHandler(NoSuchMethodException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("æªç¥æ¹æ³å¼å¸¸"); |
| | | } |
| | | |
| | | /** æ°ç»è¶çå¼å¸¸ */ |
| | | @ExceptionHandler(IndexOutOfBoundsException.class) |
| | | public Result<?> indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("æ°ç»è¶çå¼å¸¸"); |
| | | } |
| | | /** sqlè¯æ³é误å¼å¸¸ */ |
| | | @ExceptionHandler(BadSqlGrammarException.class) |
| | | public Result<?> BadSqlGrammarException(BadSqlGrammarException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("sqlè¯æ³é误å¼å¸¸"); |
| | | } |
| | | |
| | | /** æ æ³æ³¨å
¥beanå¼å¸¸ */ |
| | | @ExceptionHandler(NoSuchBeanDefinitionException.class) |
| | | public Result<?> NoSuchBeanDefinitionException(NoSuchBeanDefinitionException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("æ æ³æ³¨å
¥bean"); |
| | | } |
| | | |
| | | /** Httpæ¶æ¯ä¸å¯è¯»å¼å¸¸ */ |
| | | @ExceptionHandler({HttpMessageNotReadableException.class}) |
| | | public Result<?> requestNotReadable(HttpMessageNotReadableException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("Httpæ¶æ¯ä¸å¯è¯»"); |
| | | } |
| | | |
| | | /** 400é误 */ |
| | | @ExceptionHandler({TypeMismatchException.class}) |
| | | public Result<?> requestTypeMismatch(TypeMismatchException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("æå¡å¨å¼å¸¸"); |
| | | } |
| | | |
| | | /** 500é误 */ |
| | | @ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class}) |
| | | public Result<?> server500(RuntimeException e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("æå¡å¨å¼å¸¸"); |
| | | } |
| | | |
| | | /** æ æº¢åº */ |
| | | @ExceptionHandler({StackOverflowError.class}) |
| | | public Result<?> requestStackOverflow(StackOverflowError e) { |
| | | e.printStackTrace(); |
| | | return Result.fail("æ æº¢åºå¼å¸¸"); |
| | | } |
| | | |
| | | /** 餿°ä¸è½ä¸º0 */ |
| | | @ExceptionHandler({ArithmeticException.class}) |
| | | public Result<?> arithmeticException(ArithmeticException e) { |
| | | 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) { |
| | | e.printStackTrace(); |
| | | return Result.fail("ç½ç»è¿æ¥å¤±è´¥ï¼è¯·éåºååè¯"); |
| | | } |
| | | } |