中国算力平台算力登记系统2.0
yanzhaofeige
2024-09-30 f21d302f4ccb816ca2d411544b5ff8bd4f9d4cac
commit | author | age
43dc29 1 package com.odcc.cpzidc.framework.web.exception;
Y 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.odcc.cpzidc.common.constant.HttpStatus;
15 import com.odcc.cpzidc.common.core.domain.AjaxResult;
16 import com.odcc.cpzidc.common.core.text.Convert;
17 import com.odcc.cpzidc.common.exception.DemoModeException;
18 import com.odcc.cpzidc.common.exception.ServiceException;
19 import com.odcc.cpzidc.common.utils.StringUtils;
20 import com.odcc.cpzidc.common.utils.html.EscapeUtil;
21
22 /**
23  * 全局异常处理器
24  * 
25  * @author ruoyi
26  */
27 @RestControllerAdvice
28 public class GlobalExceptionHandler
29 {
30     private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
31
32     /**
33      * 权限校验异常
34      */
35     @ExceptionHandler(AccessDeniedException.class)
36     public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request)
37     {
38         String requestURI = request.getRequestURI();
39         log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
40         return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");
41     }
42
43     /**
44      * 请求方式不支持
45      */
46     @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
47     public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
48             HttpServletRequest request)
49     {
50         String requestURI = request.getRequestURI();
51         log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
52         return AjaxResult.error(e.getMessage());
53     }
54
55     /**
56      * 业务异常
57      */
58     @ExceptionHandler(ServiceException.class)
59     public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request)
60     {
61         log.error(e.getMessage(), e);
62         Integer code = e.getCode();
63         return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
64     }
65
66     /**
67      * 请求路径中缺少必需的路径变量
68      */
69     @ExceptionHandler(MissingPathVariableException.class)
70     public AjaxResult handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request)
71     {
72         String requestURI = request.getRequestURI();
73         log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI, e);
74         return AjaxResult.error(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName()));
75     }
76
77     /**
78      * 请求参数类型不匹配
79      */
80     @ExceptionHandler(MethodArgumentTypeMismatchException.class)
81     public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request)
82     {
83         String requestURI = request.getRequestURI();
84         String value = Convert.toStr(e.getValue());
85         if (StringUtils.isNotEmpty(value))
86         {
87             value = EscapeUtil.clean(value);
88         }
89         log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI, e);
90         return AjaxResult.error(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), value));
91     }
92
93     /**
94      * 拦截未知的运行时异常
95      */
96     @ExceptionHandler(RuntimeException.class)
97     public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
98     {
99         String requestURI = request.getRequestURI();
100         log.error("请求地址'{}',发生未知异常.", requestURI, e);
101         return AjaxResult.error(e.getMessage());
102     }
103
104     /**
105      * 系统异常
106      */
107     @ExceptionHandler(Exception.class)
108     public AjaxResult handleException(Exception e, HttpServletRequest request)
109     {
110         String requestURI = request.getRequestURI();
111         log.error("请求地址'{}',发生系统异常.", requestURI, e);
112         return AjaxResult.error(e.getMessage());
113     }
114
115     /**
116      * 自定义验证异常
117      */
118     @ExceptionHandler(BindException.class)
119     public AjaxResult handleBindException(BindException e)
120     {
121         log.error(e.getMessage(), e);
122         String message = e.getAllErrors().get(0).getDefaultMessage();
123         return AjaxResult.error(message);
124     }
125
126     /**
127      * 自定义验证异常
128      */
129     @ExceptionHandler(MethodArgumentNotValidException.class)
130     public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
131     {
132         log.error(e.getMessage(), e);
133         String message = e.getBindingResult().getFieldError().getDefaultMessage();
134         return AjaxResult.error(message);
135     }
136
137     /**
138      * 演示模式异常
139      */
140     @ExceptionHandler(DemoModeException.class)
141     public AjaxResult handleDemoModeException(DemoModeException e)
142     {
143         return AjaxResult.error("演示模式,不允许操作");
144     }
145 }