1
zhujie
5 天以前 ec15861e14c66c38b1a8f5fffc6975d7da6c315c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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);
    }
}