huanghongfa
2021-01-05 d9eee1919dbff7ffddc48b1f4fee2ec41a54a2a2
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
115
116
117
118
119
120
121
package com.panzhihua.common.exceptions;
 
import com.panzhihua.common.constants.HttpStatus;
import com.panzhihua.common.model.vos.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
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 java.util.List;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: 全局异常捕获
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-25 14:36
 **/
@Slf4j
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionCapture {
    /**
     * 拦截捕捉自定义异常 TokenException.class
     * @param ex token 异常
     * @return R 401
     */
    @ExceptionHandler(value = TokenException.class)
    public R myErrorHandlerTokenException(TokenException ex) {
        return R.fail(ex.getCode(),ex.getMsg());
    }
 
    /**
     * 拦截捕捉自定义异常 UnAuthenticationException.class
     * @param ex 认证 异常
     * @return R 401
     */
    @ExceptionHandler(value = UnAuthenticationException.class)
    public R myErrorHandlerUnAuthenticationException(UnAuthenticationException ex) {
        return R.fail(ex.getCode(),"没有登录");
    }
 
    /**
     * 拦截捕捉自定义异常 UnAuthorizationException.class
     * @param ex 权限 异常
     * @return R 403
     */
    @ExceptionHandler(value = UnAuthorizationException.class)
    public R myErrorHandlerUnAuthorizationException(UnAuthenticationException ex) {
        return R.fail(ex.getCode(),"没有访问权限");
    }
 
 
    /**
     *
     * @param ex 数据库异常
     * @return
     */
    @ExceptionHandler(value = java.sql.SQLSyntaxErrorException.class)
    public R sqlSyntaxExcetption(Exception ex) {
        log.error("数据库异常【{}】",ex.getMessage());
        return R.fail("sql语法错误");
    }
 
    /**
     *
     * @param ex 数据库异常
     * @return
     */
    @ExceptionHandler(value = java.sql.SQLException.class)
    public R sqlException(Exception ex) {
        log.error("数据库异常【{}】",ex.getMessage());
        return R.fail("sql错误");
    }
 
    /**
     *
     * @param ex 数据库异常
     * @return
     */
    @ExceptionHandler(value = ServiceException.class)
    public R serviceException(Exception ex) {
        log.error("服务层业务异常【{}】",ex.getMessage());
        return R.fail(ex.getMessage());
    }
 
    /**
     * 校验异常
     * @param ex valid
     * @return 返回json
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R methodArgumentNotValidException(MethodArgumentNotValidException ex) {
        BindingResult result = ex.getBindingResult();
        StringBuilder errorMessage = new StringBuilder();
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            errors.forEach(p ->{
                FieldError fieldError = (FieldError) p;
                errorMessage.append(fieldError.getDefaultMessage());
                errorMessage.append("  ");
            });
        }
        return R.fail(HttpStatus.BAD_REQUEST,errorMessage.toString());
    }
 
//    /**
//     * 全局异常捕捉处理
//     * @param ex 所有运行时异常
//     * @return R 500
//     */
//    @ExceptionHandler(value = Exception.class)
//    public R errorHandler(Exception ex) {
//        log.error("捕捉到全局异常【{}】",ex.getMessage());
//        return R.fail("接口请求失败");
//    }
}