skyouc
2025-04-09 faa2a5d8d3cba12ff98c261713bff4d9cea99b79
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
package com.vincent.rsf.server.common.exception;
 
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.common.constant.Constants;
import com.vincent.rsf.server.common.utils.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.servlet.http.HttpServletResponse;
 
/**
 * 全局异常处理器
 *
 * @author vincent
 * @since 2018-02-22 11:29:30
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private final Logger logger = LoggerFactory.getLogger(getClass());
 
    @ResponseBody
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R methodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e,
                                                HttpServletResponse response) {
        e.printStackTrace();
        CommonUtil.addCrossHeaders(response);
        return R.error("The request method is incorrect");
    }
 
    @ResponseBody
    @ExceptionHandler(AccessDeniedException.class)
    public R accessDeniedExceptionHandler(AccessDeniedException e, HttpServletResponse response) {
        CommonUtil.addCrossHeaders(response);
        return R.parse(Constants.UNAUTHORIZED_CODE + "-" + Constants.UNAUTHORIZED_MSG);
    }
 
    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    public R businessExceptionHandler(BusinessException e, HttpServletResponse response) {
        CommonUtil.addCrossHeaders(response);
        return R.parse(e.getMessage());
    }
 
    @ResponseBody
    @ExceptionHandler(CoolException.class)
    public R coolExceptionHandler(CoolException e, HttpServletResponse response) {
        CommonUtil.addCrossHeaders(response);
        return R.error(e.getMessage());
    }
 
 
    @ResponseBody
    @ExceptionHandler(Throwable.class)
    public R exceptionHandler(Throwable e, HttpServletResponse response) {
        logger.error(e.getMessage(), e);
        CommonUtil.addCrossHeaders(response);
        return R.error(Constants.RESULT_ERROR_MSG);
    }
 
}