|
@@ -41,27 +41,26 @@ import java.util.Map;
|
|
|
@RestControllerAdvice
|
|
|
@Slf4j
|
|
|
public class GloableExceptionResolver {
|
|
|
- @ExceptionHandler(Exception.class)
|
|
|
- public Result<?> handleException(Exception e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- return Result.error("系统异常,请稍后重试," + e.getMessage());
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
public Result<?> HttpRequestMethodNotSupportedException(Exception e) {
|
|
|
log.error(e.getMessage(), e);
|
|
|
return Result.error("请求方式异常," + e.getMessage());
|
|
|
}
|
|
|
+
|
|
|
@ExceptionHandler(DataIntegrityViolationException.class)
|
|
|
public Result<?> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
|
|
|
log.error(e.getMessage(), e);
|
|
|
return Result.error("字段太长,超出数据库字段的长度");
|
|
|
}
|
|
|
+
|
|
|
@ExceptionHandler(PoolException.class)
|
|
|
public Result<?> handlePoolException(PoolException e) {
|
|
|
log.error(e.getMessage(), e);
|
|
|
return Result.error("Redis 连接异常!");
|
|
|
}
|
|
|
+
|
|
|
@ExceptionHandler
|
|
|
public Result<?> methodArgumentNotValid(BindException e) {
|
|
|
if (log.isDebugEnabled()) {
|
|
@@ -100,6 +99,7 @@ public class GloableExceptionResolver {
|
|
|
//
|
|
|
// }
|
|
|
//
|
|
|
+
|
|
|
/**
|
|
|
* 捕捉所有Shiro异常
|
|
|
*
|
|
@@ -109,45 +109,49 @@ public class GloableExceptionResolver {
|
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
|
|
@ExceptionHandler(ShiroException.class)
|
|
|
public Result<?> handle401(ShiroException e) {
|
|
|
- return Result.error(HttpStatus.UNAUTHORIZED.value(), "无权访问:" + e.getMessage());
|
|
|
+ return Result.error(HttpStatus.UNAUTHORIZED.value(), e.getMessage());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 单独捕捉Shiro(UnauthorizedException)异常
|
|
|
* 该异常为访问有权限管控的请求而该用户没有所需权限所抛出的异常
|
|
|
+ *
|
|
|
* @param e
|
|
|
* @return
|
|
|
*/
|
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
|
|
@ExceptionHandler(UnauthorizedException.class)
|
|
|
public Result<?> handle401(UnauthorizedException e) {
|
|
|
- return Result.error(HttpStatus.UNAUTHORIZED.value(), "无权访问:当前用户没有此请求所需权限(" + e.getMessage() + ")");
|
|
|
+ return Result.error(HttpStatus.UNAUTHORIZED.value(), "当前用户没有此请求所需权限,请联系管理员" + e.getMessage() + ")");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 单独捕捉Shiro(UnauthenticatedException)异常
|
|
|
* 该异常为以游客身份访问有权限管控的请求无法对匿名主体进行授权,而授权失败所抛出的异常
|
|
|
+ *
|
|
|
* @param e
|
|
|
* @return
|
|
|
*/
|
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
|
|
@ExceptionHandler(UnauthenticatedException.class)
|
|
|
public Result<?> handle401(UnauthenticatedException e) {
|
|
|
- return Result.error(HttpStatus.UNAUTHORIZED.value(), "无权访问:请先登录");
|
|
|
+ return Result.error(HttpStatus.UNAUTHORIZED.value(), "请先登录");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 捕捉UnauthorizedException自定义异常
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
|
|
@ExceptionHandler(CustomUnauthorizedException.class)
|
|
|
public Result<?> handle401(CustomUnauthorizedException e) {
|
|
|
- return Result.error(HttpStatus.UNAUTHORIZED.value(), "无权访问:" + e.getMessage());
|
|
|
+ return Result.error(HttpStatus.UNAUTHORIZED.value(), e.getMessage());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 捕捉其他所有自定义异常
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
@@ -155,8 +159,10 @@ public class GloableExceptionResolver {
|
|
|
public Result<?> handle(CustomException e) {
|
|
|
return Result.error(HttpStatus.BAD_REQUEST.value(), e.getMessage());
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 捕捉404异常
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
|
@@ -164,6 +170,7 @@ public class GloableExceptionResolver {
|
|
|
public Result<?> handle(NoHandlerFoundException e) {
|
|
|
return Result.error(HttpStatus.NOT_FOUND.value(), e.getMessage());
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 获取状态码
|
|
|
*
|
|
@@ -178,5 +185,18 @@ public class GloableExceptionResolver {
|
|
|
return HttpStatus.valueOf(statusCode);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 捕捉其他所有异常
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param ex
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
+ @ExceptionHandler(Exception.class)
|
|
|
+ public Result<?> globalException(HttpServletRequest request, Throwable ex) {
|
|
|
+ return Result.error(this.getStatus(request).value(), ex.toString() + ": " + ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|