admin
2024-04-24 363457b34e0e4f26ffe51aa80ecb227bf7873308
提交 | 用户 | 时间
363457 1 package com.jcdm.framework.web.exception;
A 2
3 import javax.servlet.http.HttpServletRequest;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6 import org.springframework.security.access.AccessDeniedException;
7 import org.springframework.validation.BindException;
8 import org.springframework.web.HttpRequestMethodNotSupportedException;
9 import org.springframework.web.bind.MethodArgumentNotValidException;
10 import org.springframework.web.bind.MissingPathVariableException;
11 import org.springframework.web.bind.annotation.ExceptionHandler;
12 import org.springframework.web.bind.annotation.RestControllerAdvice;
13 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
14 import com.jcdm.common.constant.HttpStatus;
15 import com.jcdm.common.core.domain.AjaxResult;
16 import com.jcdm.common.exception.DemoModeException;
17 import com.jcdm.common.exception.ServiceException;
18 import com.jcdm.common.utils.StringUtils;
19
20 /**
21  * 全局异常处理器
22  * 
23  * @author jc
24  */
25 @RestControllerAdvice
26 public class GlobalExceptionHandler
27 {
28     private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
29
30     /**
31      * 权限校验异常
32      */
33     @ExceptionHandler(AccessDeniedException.class)
34     public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request)
35     {
36         String requestURI = request.getRequestURI();
37         log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
38         return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");
39     }
40
41     /**
42      * 请求方式不支持
43      */
44     @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
45     public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
46             HttpServletRequest request)
47     {
48         String requestURI = request.getRequestURI();
49         log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
50         return AjaxResult.error(e.getMessage());
51     }
52
53     /**
54      * 业务异常
55      */
56     @ExceptionHandler(ServiceException.class)
57     public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request)
58     {
59         log.error(e.getMessage(), e);
60         Integer code = e.getCode();
61         return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
62     }
63
64     /**
65      * 请求路径中缺少必需的路径变量
66      */
67     @ExceptionHandler(MissingPathVariableException.class)
68     public AjaxResult handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request)
69     {
70         String requestURI = request.getRequestURI();
71         log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI, e);
72         return AjaxResult.error(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName()));
73     }
74
75     /**
76      * 请求参数类型不匹配
77      */
78     @ExceptionHandler(MethodArgumentTypeMismatchException.class)
79     public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request)
80     {
81         String requestURI = request.getRequestURI();
82         log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI, e);
83         return AjaxResult.error(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue()));
84     }
85
86     /**
87      * 拦截未知的运行时异常
88      */
89     @ExceptionHandler(RuntimeException.class)
90     public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
91     {
92         String requestURI = request.getRequestURI();
93         log.error("请求地址'{}',发生未知异常.", requestURI, e);
94         return AjaxResult.error(e.getMessage());
95     }
96
97     /**
98      * 系统异常
99      */
100     @ExceptionHandler(Exception.class)
101     public AjaxResult handleException(Exception e, HttpServletRequest request)
102     {
103         String requestURI = request.getRequestURI();
104         log.error("请求地址'{}',发生系统异常.", requestURI, e);
105         return AjaxResult.error(e.getMessage());
106     }
107
108     /**
109      * 自定义验证异常
110      */
111     @ExceptionHandler(BindException.class)
112     public AjaxResult handleBindException(BindException e)
113     {
114         log.error(e.getMessage(), e);
115         String message = e.getAllErrors().get(0).getDefaultMessage();
116         return AjaxResult.error(message);
117     }
118
119     /**
120      * 自定义验证异常
121      */
122     @ExceptionHandler(MethodArgumentNotValidException.class)
123     public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
124     {
125         log.error(e.getMessage(), e);
126         String message = e.getBindingResult().getFieldError().getDefaultMessage();
127         return AjaxResult.error(message);
128     }
129
130     /**
131      * 演示模式异常
132      */
133     @ExceptionHandler(DemoModeException.class)
134     public AjaxResult handleDemoModeException(DemoModeException e)
135     {
136         return AjaxResult.error("演示模式,不允许操作");
137     }
138 }