|
@@ -1,19 +1,19 @@
|
|
|
package com.usky.exception;
|
|
|
|
|
|
import com.usky.utils.Result;
|
|
|
-import com.usky.utils.ServletUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.shiro.ShiroException;
|
|
|
+import org.apache.shiro.authc.IncorrectCredentialsException;
|
|
|
+import org.apache.shiro.authc.LockedAccountException;
|
|
|
+import org.apache.shiro.authc.UnknownAccountException;
|
|
|
import org.apache.shiro.authz.AuthorizationException;
|
|
|
-import org.apache.shiro.util.PermissionUtils;
|
|
|
-import org.springframework.validation.BindException;
|
|
|
-import org.springframework.validation.BindingResult;
|
|
|
-import org.springframework.validation.FieldError;
|
|
|
+import org.apache.shiro.authz.UnauthorizedException;
|
|
|
+import org.springframework.dao.DataIntegrityViolationException;
|
|
|
+import org.springframework.data.redis.connection.PoolException;
|
|
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
-import org.springframework.web.servlet.ModelAndView;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
+import org.springframework.web.servlet.NoHandlerFoundException;
|
|
|
|
|
|
/**
|
|
|
* @author laowo
|
|
@@ -24,72 +24,44 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
@RestControllerAdvice
|
|
|
@Slf4j
|
|
|
public class GloableExceptionResolver {
|
|
|
- /**
|
|
|
- * 权限校验失败 如果请求为ajax返回json,普通请求跳转页面
|
|
|
- */
|
|
|
- @ExceptionHandler(AuthorizationException.class)
|
|
|
- public Object handleAuthorizationException(HttpServletRequest request, AuthorizationException e) {
|
|
|
+
|
|
|
+ @ExceptionHandler(Exception.class)
|
|
|
+ public Result<?> handleException(Exception e) {
|
|
|
log.error(e.getMessage(), e);
|
|
|
- if (ServletUtils.isAjaxRequest(request)) {
|
|
|
- return Result.error("权限错误!");
|
|
|
- } else {
|
|
|
-// ModelAndView modelAndView = new ModelAndView();
|
|
|
-// modelAndView.setViewName("error/unauth");
|
|
|
-// return modelAndView;
|
|
|
- return Result.error("权限错误!");
|
|
|
- }
|
|
|
+ return Result.error("操作失败," + e.getMessage());
|
|
|
}
|
|
|
- /**
|
|
|
- * 参数校验异常
|
|
|
- */
|
|
|
- @ExceptionHandler(value = BindException.class)
|
|
|
- public Result<?> validationExceptionHandler(BindException e) {
|
|
|
- BindingResult bindingResult = e.getBindingResult();
|
|
|
- String errorMesssage = "";
|
|
|
- for (FieldError fieldError : bindingResult.getFieldErrors()) {
|
|
|
- errorMesssage += fieldError.getDefaultMessage() + "!";
|
|
|
- }
|
|
|
- return Result.error(errorMesssage);
|
|
|
- }
|
|
|
- /**
|
|
|
- * 请求方式不支持
|
|
|
- */
|
|
|
- @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
|
|
|
- public Result handleException(HttpRequestMethodNotSupportedException e) {
|
|
|
+ @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
+ public Result<?> HttpRequestMethodNotSupportedException(Exception e) {
|
|
|
log.error(e.getMessage(), e);
|
|
|
- return Result.error("不支持' " + e.getMethod() + "'请求");
|
|
|
- }
|
|
|
- /**
|
|
|
- * 拦截未知的运行时异常
|
|
|
- */
|
|
|
- @ExceptionHandler(RuntimeException.class)
|
|
|
- public Result notFount(RuntimeException e) {
|
|
|
- log.error("运行时异常:", e);
|
|
|
- return Result.error("运行时异常:" + e.getMessage());
|
|
|
+ return Result.error("请求方式异常," + e.getMessage());
|
|
|
}
|
|
|
- /**
|
|
|
- * 系统异常
|
|
|
- */
|
|
|
- @ExceptionHandler(Exception.class)
|
|
|
- public Result handleException(Exception e) {
|
|
|
+ @ExceptionHandler(DataIntegrityViolationException.class)
|
|
|
+ public Result<?> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
|
|
|
log.error(e.getMessage(), e);
|
|
|
- return Result.error("服务器错误,请联系管理员");
|
|
|
+ return Result.error("字段太长,超出数据库字段的长度");
|
|
|
}
|
|
|
- /**
|
|
|
- * 业务异常
|
|
|
- */
|
|
|
- @ExceptionHandler(BusinessException.class)
|
|
|
- public Object businessException(HttpServletRequest request, BusinessException e) {
|
|
|
+
|
|
|
+ @ExceptionHandler(PoolException.class)
|
|
|
+ public Result<?> handlePoolException(PoolException e) {
|
|
|
log.error(e.getMessage(), e);
|
|
|
- if (ServletUtils.isAjaxRequest(request)) {
|
|
|
- return Result.error(e.getMessage());
|
|
|
- } else {
|
|
|
- // ModelAndView modelAndView = new ModelAndView();
|
|
|
- // modelAndView.addObject("errorMessage", e.getMessage());
|
|
|
- // modelAndView.setViewName("error/business");
|
|
|
- // return modelAndView;
|
|
|
- return Result.error(e.getMessage());
|
|
|
+ return Result.error("Redis 连接异常!");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @ExceptionHandler(ShiroException.class)
|
|
|
+ public Result<?> doHandleShiroException(
|
|
|
+ ShiroException e) {
|
|
|
+ if (e instanceof UnknownAccountException) {
|
|
|
+ return Result.error("账户不存在");
|
|
|
+ } else if (e instanceof LockedAccountException) {
|
|
|
+ return Result.error("账户被禁用");
|
|
|
+ } else if (e instanceof IncorrectCredentialsException) {
|
|
|
+ return Result.error("密码不正确");
|
|
|
+ } else if (e instanceof AuthorizationException) {
|
|
|
+ return Result.error("没有此操作权限");
|
|
|
}
|
|
|
+ return Result.error("系统维护中");
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|