| package com.mzl.flower.config; | 
|   | 
| import com.mzl.flower.base.R; | 
| import com.mzl.flower.base.ReturnDataDTO; | 
| import com.mzl.flower.config.exception.BaseException; | 
| import com.mzl.flower.config.exception.ValidationException; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.context.MessageSource; | 
| import org.springframework.context.i18n.LocaleContextHolder; | 
| import org.springframework.http.HttpStatus; | 
| import org.springframework.http.ResponseEntity; | 
| import org.springframework.validation.BindException; | 
| import org.springframework.validation.FieldError; | 
| import org.springframework.web.bind.MethodArgumentNotValidException; | 
| import org.springframework.web.bind.annotation.ExceptionHandler; | 
| import org.springframework.web.bind.annotation.RestControllerAdvice; | 
|   | 
| import javax.validation.ConstraintViolationException; | 
| import java.util.ArrayList; | 
| import java.util.HashMap; | 
| import java.util.List; | 
| import java.util.Map; | 
|   | 
| import static com.mzl.flower.base.R.RUNTIME_EXCEPTION; | 
| import static com.mzl.flower.base.R.VALIDATION_ERROR; | 
|   | 
|   | 
| @Slf4j | 
| @RestControllerAdvice | 
| public class ResourceExceptionHandler { | 
|     @Autowired | 
|     private MessageSource messageSource; | 
|   | 
|     @ExceptionHandler(value = MethodArgumentNotValidException.class) | 
|     public ResponseEntity<?> handleValidationException(MethodArgumentNotValidException exception) { | 
|         List<Map<String, String>> mapList = new ArrayList<>(); | 
|         exception.getBindingResult().getAllErrors().forEach((error) -> { | 
|             Map<String, String> errorMap = new HashMap<>(); | 
|             String fieldName = ((FieldError) error).getField(); | 
|             String errorMessage = error.getDefaultMessage(); | 
|             errorMap.put("fieldName", fieldName); | 
|             errorMap.put("errorMessage", errorMessage); | 
|             mapList.add(errorMap); | 
|         }); | 
|         return new ResponseEntity<>(new ReturnDataDTO<>(VALIDATION_ERROR.getCode(), mapList, "error"), HttpStatus.OK); | 
|     } | 
|   | 
|     @ExceptionHandler(value = BindException.class) | 
|     public ResponseEntity<?> handleValidationException(BindException exception) { | 
|         List<Map<String, String>> mapList = new ArrayList<>(); | 
|         exception.getBindingResult().getAllErrors().forEach((error) -> { | 
|             Map<String, String> errorMap = new HashMap<>(); | 
|             String fieldName = ((FieldError) error).getField(); | 
|             String errorMessage = error.getDefaultMessage(); | 
|             errorMap.put("fieldName", fieldName); | 
|             errorMap.put("errorMessage", errorMessage); | 
|             mapList.add(errorMap); | 
|         }); | 
|         return new ResponseEntity<>(new ReturnDataDTO<>(VALIDATION_ERROR.getCode(), mapList, "error"), HttpStatus.OK); | 
|     } | 
|   | 
|     @ExceptionHandler(value = ConstraintViolationException.class) | 
|     public ResponseEntity<?> handleValidationException(ConstraintViolationException exception) { | 
|         List<Map<String, String>> mapList = new ArrayList<>(); | 
|         exception.getConstraintViolations().forEach((error) -> { | 
|             Map<String, String> errorMap = new HashMap<>(); | 
|             String errorMessage = error.getMessage(); | 
|             String path = error.getPropertyPath().toString(); | 
|             String fieldName = path.substring(path.lastIndexOf(".") + 1); | 
|             errorMap.put("fieldName", fieldName); | 
|             errorMap.put("errorMessage", errorMessage); | 
|             mapList.add(errorMap); | 
|         }); | 
|         return new ResponseEntity<>(new ReturnDataDTO<>(VALIDATION_ERROR.getCode(), mapList, "error"), HttpStatus.OK); | 
|     } | 
|   | 
|     @ExceptionHandler(value = ValidationException.class) | 
|     public ResponseEntity<?> handleValidationException(ValidationException exception) { | 
|         Map<String, String> errorMap = new HashMap<>(); | 
| //        errorMap.put("fieldName", exception.getFieldName()); | 
|         String errorMessage = messageSource.getMessage(exception.getErrorMessageKey(), exception.getErrorMessageArgs(), | 
|                 exception.getErrorMessageKey(), LocaleContextHolder.getLocale()); | 
|         errorMap.put("errorMessage", errorMessage); | 
|         return new ResponseEntity<>(new ReturnDataDTO<>(VALIDATION_ERROR.getCode(), null, errorMessage), HttpStatus.OK); | 
|     } | 
|   | 
|     @ExceptionHandler(value = BaseException.class) | 
|     public ResponseEntity<?> handleValidationException(BaseException exception) { | 
|         log.error(exception.getMessage(),exception); | 
|         String errorMessage = messageSource.getMessage(exception.getErrorMessageKey(), exception.getErrorMessageArgs(), | 
|                 exception.getErrorMessageKey(), LocaleContextHolder.getLocale()); | 
|         return new ResponseEntity<>(new ReturnDataDTO<>(exception.getErrorCode(), null, errorMessage), HttpStatus.OK); | 
|     } | 
|   | 
|     @ExceptionHandler(value = Exception.class) | 
|     public ResponseEntity<?> handleException(Exception exception) { | 
|         log.error(exception.getMessage(),exception); | 
|         ReturnDataDTO returnResultDTO = new ReturnDataDTO(); | 
|         String errorName = exception.getClass().getName(); | 
|         errorName = errorName.substring(errorName.lastIndexOf(".") + 1); | 
|         if (R.contains(errorName)) { | 
|             returnResultDTO.setCode(R.valueOf(errorName).getCode()); | 
|             String errorMessage = messageSource.getMessage(R.valueOf(errorName).getMsg(), null, | 
|                     R.valueOf(errorName).getMsg(), LocaleContextHolder.getLocale()); | 
|             returnResultDTO.setData(errorMessage); | 
|         } else { | 
|             returnResultDTO.setCode(RUNTIME_EXCEPTION.getCode()); | 
|         } | 
|         return new ResponseEntity<>(returnResultDTO, HttpStatus.OK); | 
|     } | 
| } |