xuefei
2020-12-10 eeeb7233935ea9b10e99043bdbf740ef86c9bf20
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
113
114
package cn.cetc54.platform.core.common.exception;
 
import cn.cetc54.platform.core.common.utils.ResultUtil;
import cn.cetc54.platform.core.common.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
 
/**
 * @author
 */
@Slf4j
@RestControllerAdvice
public class RestCtrlExceptionHandler {
 
    @ExceptionHandler(PlatformException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Result<Object> handlePlatformException(PlatformException e) {
 
        String errorMsg = "Platform exception";
        if (e!=null){
            errorMsg = e.getMsg();
            log.error(e.toString(), e);
        }
        return new ResultUtil<>().setErrorMsg(500, errorMsg);
    }
 
    @ExceptionHandler(BindException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Result<Object> handleBindException(BindException e) {
 
        StringBuilder sb = new StringBuilder();
        e.getFieldErrors().forEach(error->{
            String fieldName = error.getField();
            String message = error.getDefaultMessage();
            sb.append(fieldName + "-" + message + ";");
        });
        String result = sb.toString();
        if(result.length()>0){
            result = result.substring(0, result.length()-1);
        }
        return new ResultUtil<>().setErrorMsg(500, result);
    }
 
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Result<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
 
        StringBuilder sb = new StringBuilder();
        e.getBindingResult().getFieldErrors().forEach(error->{
            String fieldName = error.getField();
            String message = error.getDefaultMessage();
            sb.append(fieldName + "-" + message + ";");
        });
        String result = sb.toString();
        if(result.length()>0){
            result = result.substring(0, result.length()-1);
        }
        return new ResultUtil<>().setErrorMsg(500, result);
    }
 
    @ExceptionHandler(LimitException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Result<Object> handleLimitException(LimitException e) {
 
        String errorMsg = "Limit exception";
        if (e!=null){
            errorMsg = e.getMsg();
            log.warn(e.getMsg(), e);
        }
        return new ResultUtil<>().setErrorMsg(500, errorMsg);
    }
 
    @ExceptionHandler(CaptchaException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Result<Object> handleCaptchaExceptionException(CaptchaException e) {
 
        String errorMsg = "CaptchaException exception";
        if (e!=null){
            errorMsg = e.getMsg();
            log.warn(e.getMsg(), e);
        }
        return new ResultUtil<>().setErrorMsg(500, errorMsg);
    }
 
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Result<Object> handleAccessDeniedExceptionException(AccessDeniedException e) {
 
        String errorMsg = "AccessDeniedException exception";
        if (e!=null){
            errorMsg = e.getMessage();
            log.warn(e.getMessage(), e);
        }
        return new ResultUtil<>().setErrorMsg(500, errorMsg);
    }
 
    @ExceptionHandler(java.lang.Exception.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Result<Object> handleException(java.lang.Exception e) {
 
        String errorMsg = "Exception";
        if (e!=null){
            errorMsg = e.getMessage();
            log.error(e.toString(), e);
        }
        return new ResultUtil<>().setErrorMsg(500, errorMsg);
    }
}