|
@@ -22,6 +22,8 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
|
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
/**
|
|
|
* 全局异常处理器
|
|
|
*
|
|
@@ -36,8 +38,8 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(NotPermissionException.class)
|
|
|
public CommonResult<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
|
|
|
- String requestURI = request.getRequestURI();
|
|
|
- log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
|
|
|
+ String requestUri = request.getRequestURI();
|
|
|
+ log.error("请求地址'{}',权限码校验失败'{}'", requestUri, e.getMessage());
|
|
|
return CommonResult.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权");
|
|
|
}
|
|
|
|
|
@@ -46,8 +48,8 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(NotRoleException.class)
|
|
|
public CommonResult<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) {
|
|
|
- String requestURI = request.getRequestURI();
|
|
|
- log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
|
|
|
+ String requestUri = request.getRequestURI();
|
|
|
+ log.error("请求地址'{}',角色权限校验失败'{}'", requestUri, e.getMessage());
|
|
|
return CommonResult.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权");
|
|
|
}
|
|
|
|
|
@@ -56,8 +58,8 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(NotLoginException.class)
|
|
|
public CommonResult<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
|
|
|
- String requestURI = request.getRequestURI();
|
|
|
- log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
|
|
|
+ String requestUri = request.getRequestURI();
|
|
|
+ log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestUri, e.getMessage());
|
|
|
return CommonResult.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
|
|
|
}
|
|
|
|
|
@@ -67,8 +69,8 @@ public class GlobalExceptionHandler {
|
|
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
public CommonResult<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
|
|
|
HttpServletRequest request) {
|
|
|
- String requestURI = request.getRequestURI();
|
|
|
- log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
|
|
|
+ String requestUri = request.getRequestURI();
|
|
|
+ log.error("请求地址'{}',不支持'{}'请求", requestUri, e.getMethod());
|
|
|
return CommonResult.fail(e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -96,8 +98,8 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(MissingPathVariableException.class)
|
|
|
public CommonResult<Void> handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request) {
|
|
|
- String requestURI = request.getRequestURI();
|
|
|
- log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI);
|
|
|
+ String requestUri = request.getRequestURI();
|
|
|
+ log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestUri);
|
|
|
return CommonResult.fail(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName()));
|
|
|
}
|
|
|
|
|
@@ -106,9 +108,9 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
|
|
public CommonResult<Void> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) {
|
|
|
- String requestURI = request.getRequestURI();
|
|
|
- log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI);
|
|
|
- return CommonResult.fail(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue()));
|
|
|
+ String requestUri = request.getRequestURI();
|
|
|
+ log.error("请求参数类型不匹配'{}',发生系统异常.", requestUri);
|
|
|
+ return CommonResult.fail(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), Objects.requireNonNull(e.getRequiredType()).getName(), e.getValue()));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -116,8 +118,8 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(RuntimeException.class)
|
|
|
public CommonResult<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
|
|
|
- String requestURI = request.getRequestURI();
|
|
|
- log.error("请求地址'{}',发生未知异常.", requestURI, e);
|
|
|
+ String requestUri = request.getRequestURI();
|
|
|
+ log.error("请求地址'{}',发生未知异常.", requestUri, e);
|
|
|
return CommonResult.fail(e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -126,8 +128,8 @@ public class GlobalExceptionHandler {
|
|
|
*/
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
public CommonResult<Void> handleException(Exception e, HttpServletRequest request) {
|
|
|
- String requestURI = request.getRequestURI();
|
|
|
- log.error("请求地址'{}',发生系统异常.", requestURI, e);
|
|
|
+ String requestUri = request.getRequestURI();
|
|
|
+ log.error("请求地址'{}',发生系统异常.", requestUri, e);
|
|
|
return CommonResult.fail(e.getMessage());
|
|
|
}
|
|
|
|
|
@@ -157,7 +159,7 @@ public class GlobalExceptionHandler {
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
public CommonResult<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
|
|
log.error(e.getMessage());
|
|
|
- String message = e.getBindingResult().getFieldError().getDefaultMessage();
|
|
|
+ String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
|
|
|
return CommonResult.fail(message);
|
|
|
}
|
|
|
}
|