异常信息处理
This commit is contained in:
@@ -70,6 +70,10 @@ swagger:
|
|||||||
pathMapping: /prod-api
|
pathMapping: /prod-api
|
||||||
#################################### Swagger end ###################################
|
#################################### Swagger end ###################################
|
||||||
|
|
||||||
|
server:
|
||||||
|
error:
|
||||||
|
include-stacktrace: never # 永不输出 trace 信息
|
||||||
|
|
||||||
# 防止XSS攻击
|
# 防止XSS攻击
|
||||||
xss:
|
xss:
|
||||||
# 过滤开关
|
# 过滤开关
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.vetti.framework.web.exception;
|
package com.vetti.framework.web.exception;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.security.access.AccessDeniedException;
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
@@ -29,13 +31,15 @@ public class GlobalExceptionHandler
|
|||||||
{
|
{
|
||||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 权限校验异常
|
* 权限校验异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(AccessDeniedException.class)
|
@ExceptionHandler(AccessDeniedException.class)
|
||||||
public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request)
|
public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request, HttpServletResponse response)
|
||||||
{
|
{
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
|
response.setStatus(HttpStatus.FORBIDDEN);
|
||||||
log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
|
log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
|
||||||
return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");
|
return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");
|
||||||
}
|
}
|
||||||
@@ -45,10 +49,11 @@ public class GlobalExceptionHandler
|
|||||||
*/
|
*/
|
||||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||||
public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
|
public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
|
||||||
HttpServletRequest request)
|
HttpServletRequest request, HttpServletResponse response)
|
||||||
{
|
{
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
|
log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
|
||||||
|
response.setStatus(HttpStatus.ERROR);
|
||||||
return AjaxResult.error(e.getMessage());
|
return AjaxResult.error(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,10 +61,11 @@ public class GlobalExceptionHandler
|
|||||||
* 业务异常
|
* 业务异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(ServiceException.class)
|
@ExceptionHandler(ServiceException.class)
|
||||||
public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request)
|
public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request, HttpServletResponse response)
|
||||||
{
|
{
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
Integer code = e.getCode();
|
Integer code = e.getCode();
|
||||||
|
response.setStatus(code);
|
||||||
return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
|
return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,10 +73,11 @@ public class GlobalExceptionHandler
|
|||||||
* 请求路径中缺少必需的路径变量
|
* 请求路径中缺少必需的路径变量
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(MissingPathVariableException.class)
|
@ExceptionHandler(MissingPathVariableException.class)
|
||||||
public AjaxResult handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request)
|
public AjaxResult handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request, HttpServletResponse response)
|
||||||
{
|
{
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI, e);
|
log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI, e);
|
||||||
|
response.setStatus(HttpStatus.ERROR);
|
||||||
return AjaxResult.error(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName()));
|
return AjaxResult.error(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +85,7 @@ public class GlobalExceptionHandler
|
|||||||
* 请求参数类型不匹配
|
* 请求参数类型不匹配
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
||||||
public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request)
|
public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request, HttpServletResponse response)
|
||||||
{
|
{
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
String value = Convert.toStr(e.getValue());
|
String value = Convert.toStr(e.getValue());
|
||||||
@@ -86,6 +93,7 @@ public class GlobalExceptionHandler
|
|||||||
{
|
{
|
||||||
value = EscapeUtil.clean(value);
|
value = EscapeUtil.clean(value);
|
||||||
}
|
}
|
||||||
|
response.setStatus(HttpStatus.ERROR);
|
||||||
log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI, e);
|
log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI, e);
|
||||||
return AjaxResult.error(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), value));
|
return AjaxResult.error(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), value));
|
||||||
}
|
}
|
||||||
@@ -94,10 +102,11 @@ public class GlobalExceptionHandler
|
|||||||
* 拦截未知的运行时异常
|
* 拦截未知的运行时异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(RuntimeException.class)
|
@ExceptionHandler(RuntimeException.class)
|
||||||
public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
|
public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request, HttpServletResponse response)
|
||||||
{
|
{
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
log.error("请求地址'{}',发生未知异常.", requestURI, e);
|
log.error("请求地址'{}',发生未知异常.", requestURI, e);
|
||||||
|
response.setStatus(HttpStatus.ERROR);
|
||||||
return AjaxResult.error(e.getMessage());
|
return AjaxResult.error(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,10 +114,11 @@ public class GlobalExceptionHandler
|
|||||||
* 系统异常
|
* 系统异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public AjaxResult handleException(Exception e, HttpServletRequest request)
|
public AjaxResult handleException(Exception e, HttpServletRequest request, HttpServletResponse response)
|
||||||
{
|
{
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
log.error("请求地址'{}',发生系统异常.", requestURI, e);
|
log.error("请求地址'{}',发生系统异常.", requestURI, e);
|
||||||
|
response.setStatus(HttpStatus.ERROR);
|
||||||
return AjaxResult.error(e.getMessage());
|
return AjaxResult.error(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,9 +126,10 @@ public class GlobalExceptionHandler
|
|||||||
* 自定义验证异常
|
* 自定义验证异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(BindException.class)
|
@ExceptionHandler(BindException.class)
|
||||||
public AjaxResult handleBindException(BindException e)
|
public AjaxResult handleBindException(BindException e, HttpServletResponse response)
|
||||||
{
|
{
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
|
response.setStatus(HttpStatus.ERROR);
|
||||||
String message = e.getAllErrors().get(0).getDefaultMessage();
|
String message = e.getAllErrors().get(0).getDefaultMessage();
|
||||||
return AjaxResult.error(message);
|
return AjaxResult.error(message);
|
||||||
}
|
}
|
||||||
@@ -127,9 +138,10 @@ public class GlobalExceptionHandler
|
|||||||
* 自定义验证异常
|
* 自定义验证异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
|
public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletResponse response)
|
||||||
{
|
{
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
|
response.setStatus(HttpStatus.ERROR);
|
||||||
String message = e.getBindingResult().getFieldError().getDefaultMessage();
|
String message = e.getBindingResult().getFieldError().getDefaultMessage();
|
||||||
return AjaxResult.error(message);
|
return AjaxResult.error(message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ public class SysRegisterService
|
|||||||
//方便测试app的让过
|
//方便测试app的让过
|
||||||
if (!loginWhitelist.contains(registerBody.getUsername())) {
|
if (!loginWhitelist.contains(registerBody.getUsername())) {
|
||||||
throw new ServiceException(MessageUtils.messageCustomize("systemExceptionSysAppLoginServiceImpl10005"));
|
throw new ServiceException(MessageUtils.messageCustomize("systemExceptionSysAppLoginServiceImpl10005"));
|
||||||
|
// throw new IllegalArgumentException(MessageUtils.messageCustomize("systemExceptionSysAppLoginServiceImpl10005"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user