heheng
6 天以前 61f6970c48d5334ca2b76e90eff94b46f593f5bd
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
package com.gkhy.exam.framework.exception;
 
import com.gkhy.exam.common.api.CommonResult;
import com.gkhy.exam.common.api.ResultCode;
import com.gkhy.exam.common.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
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.servlet.http.HttpServletRequest;
import java.sql.SQLException;
 
/**
 * 全局异常处理类
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @Autowired
    HttpServletRequest request;
 
    @ExceptionHandler(value = ApiException.class)
    public CommonResult handle(ApiException e){
        writeExceptionLogFile(e);
        if(e.getErrorCode()!=null){
            return CommonResult.failed(e.getErrorCode());
        }
        return CommonResult.failed(e.getMessage());
    }
 
 
 
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public CommonResult handleValidException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getField()+fieldError.getDefaultMessage();
            }
        }
        writeExceptionLogFile(e);
        return CommonResult.validateFailed(message);
    }
 
    @ExceptionHandler(value = BindException.class)
    public CommonResult handleValidException(BindException e) {
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getField()+fieldError.getDefaultMessage();
            }
        }
        writeExceptionLogFile(e);
        return CommonResult.validateFailed(message);
    }
 
 
    @ExceptionHandler(SQLException.class)
    public CommonResult sqlExceptionHandler(Exception ex) {
        writeExceptionLogFile(ex);
        return CommonResult.failed( "执行sql异常");
    }
 
 
    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public CommonResult handleRuntimeException(RuntimeException ex, HttpServletRequest request)
    {
        writeExceptionLogFile(ex);
        return CommonResult.failed("内部服务异常");
    }
 
    /**
     * 拦截Spring secrity AccessDenied 异常
     */
    @ExceptionHandler(AccessDeniedException.class)
    public CommonResult handleAccessDeniedException(RuntimeException ex, HttpServletRequest request)
    {
        writeExceptionLogFile(ex);
        return CommonResult.failed(ResultCode.FORBIDDEN,"权限不足,无法访问系统资源");
    }
 
    /**
     * 500 其他服务器异常
     */
    @ExceptionHandler(Exception.class)
    public CommonResult otherExceptionHandler(Exception ex) {
        String message = !StringUtils.isEmpty(ex.getMessage()) ? ex.getMessage() : ex.toString();
        if(StringUtils.isNotBlank(message)&&message.contains("java.sql")){
            message="执行sql异常";
        }
        writeExceptionLogFile(ex);
        return CommonResult.failed(message);
    }
 
    private void writeExceptionLogFile(Exception ex) {
        String url="";
        if (request!=null) {
            url=request.getRequestURI();
        }
        log.error("error={}",ex);
        log.error(ex.getMessage()+",url={}",url);
    }
}