diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/demo/controller/DemoOperateController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/demo/controller/DemoOperateController.java index c7544a32..95412d84 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/demo/controller/DemoOperateController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/demo/controller/DemoOperateController.java @@ -18,7 +18,7 @@ import com.ruoyi.common.core.page.PageDomain; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.core.page.TableSupport; import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.exception.BusinessException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.web.controller.demo.domain.CustomerModel; @@ -265,7 +265,7 @@ public class DemoOperateController extends BaseController { if (StringUtils.isNull(userList) || userList.size() == 0) { - throw new BusinessException("导入用户数据不能为空!"); + throw new ServiceException("导入用户数据不能为空!"); } int successNum = 0; int failureNum = 0; @@ -315,7 +315,7 @@ public class DemoOperateController extends BaseController if (failureNum > 0) { failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); - throw new BusinessException(failureMsg.toString()); + throw new ServiceException(failureMsg.toString()); } else { diff --git a/ruoyi-admin/src/main/resources/templates/error/business.html b/ruoyi-admin/src/main/resources/templates/error/service.html similarity index 94% rename from ruoyi-admin/src/main/resources/templates/error/business.html rename to ruoyi-admin/src/main/resources/templates/error/service.html index 541fd5f2..b64341d3 100644 --- a/ruoyi-admin/src/main/resources/templates/error/business.html +++ b/ruoyi-admin/src/main/resources/templates/error/service.html @@ -3,7 +3,7 @@ - RuoYi - 403 + RuoYi - 500 diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/exception/BusinessException.java b/ruoyi-common/src/main/java/com/ruoyi/common/exception/BusinessException.java deleted file mode 100644 index d490675e..00000000 --- a/ruoyi-common/src/main/java/com/ruoyi/common/exception/BusinessException.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.ruoyi.common.exception; - -/** - * 业务异常 - * - * @author ruoyi - */ -public class BusinessException extends RuntimeException -{ - private static final long serialVersionUID = 1L; - - protected final String message; - - public BusinessException(String message) - { - this.message = message; - } - - public BusinessException(String message, Throwable e) - { - super(message, e); - this.message = message; - } - - @Override - public String getMessage() - { - return message; - } -} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/exception/GlobalException.java b/ruoyi-common/src/main/java/com/ruoyi/common/exception/GlobalException.java new file mode 100644 index 00000000..318b9aec --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/exception/GlobalException.java @@ -0,0 +1,58 @@ +package com.ruoyi.common.exception; + +/** + * 全局异常 + * + * @author ruoyi + */ +public class GlobalException extends RuntimeException +{ + + private static final long serialVersionUID = 1L; + + /** + * 错误提示 + */ + private String message; + + /** + * 错误明细,内部调试错误 + * + * 和 {@link CommonResult#getDetailMessage()} 一致的设计 + */ + private String detailMessage; + + /** + * 空构造方法,避免反序列化问题 + */ + public GlobalException() + { + } + + public GlobalException(String message) + { + this.message = message; + } + + public String getDetailMessage() + { + return detailMessage; + } + + public GlobalException setDetailMessage(String detailMessage) + { + this.detailMessage = detailMessage; + return this; + } + + public String getMessage() + { + return message; + } + + public GlobalException setMessage(String message) + { + this.message = message; + return this; + } +} \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/exception/ServiceException.java b/ruoyi-common/src/main/java/com/ruoyi/common/exception/ServiceException.java new file mode 100644 index 00000000..bd259221 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/exception/ServiceException.java @@ -0,0 +1,57 @@ +package com.ruoyi.common.exception; + +/** + * 业务异常 + * + * @author ruoyi + */ +public final class ServiceException extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + /** + * 错误提示 + */ + private String message; + + /** + * 错误明细,内部调试错误 + * + * 和 {@link CommonResult#getDetailMessage()} 一致的设计 + */ + private String detailMessage; + + /** + * 空构造方法,避免反序列化问题 + */ + public ServiceException() + { + } + + public ServiceException(String message) + { + this.message = message; + } + + public String getDetailMessage() + { + return detailMessage; + } + + public ServiceException setDetailMessage(String detailMessage) + { + this.detailMessage = detailMessage; + return this; + } + + public String getMessage() + { + return message; + } + + public ServiceException setMessage(String message) + { + this.message = message; + return this; + } +} \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index b63fad3c..4862a4bc 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -66,7 +66,7 @@ import com.ruoyi.common.annotation.Excels; import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.exception.BusinessException; +import com.ruoyi.common.exception.UtilException; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.DictUtils; import com.ruoyi.common.utils.StringUtils; @@ -450,7 +450,7 @@ public class ExcelUtil catch (Exception e) { log.error("导出Excel异常{}", e.getMessage()); - throw new BusinessException("导出Excel失败,请联系网站管理员!"); + throw new UtilException("导出Excel失败,请联系网站管理员!"); } finally { diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java index d9812d8a..e3159069 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java @@ -10,8 +10,8 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.ModelAndView; import com.ruoyi.common.core.domain.AjaxResult; -import com.ruoyi.common.exception.BusinessException; import com.ruoyi.common.exception.DemoModeException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.security.PermissionUtils; @@ -26,63 +26,62 @@ public class GlobalExceptionHandler private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); /** - * 权限校验失败 如果请求为ajax返回json,普通请求跳转页面 + * 权限校验异常(ajax请求返回json,redirect请求跳转页面) */ @ExceptionHandler(AuthorizationException.class) - public Object handleAuthorizationException(HttpServletRequest request, AuthorizationException e) + public Object handleAuthorizationException(AuthorizationException e, HttpServletRequest request) { - log.error(e.getMessage(), e); + String requestURI = request.getRequestURI(); + log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage()); if (ServletUtils.isAjaxRequest(request)) { return AjaxResult.error(PermissionUtils.getMsg(e.getMessage())); } else { - ModelAndView modelAndView = new ModelAndView(); - modelAndView.setViewName("error/unauth"); - return modelAndView; + return new ModelAndView("error/unauth"); } } /** * 请求方式不支持 */ - @ExceptionHandler({ HttpRequestMethodNotSupportedException.class }) - public AjaxResult handleException(HttpRequestMethodNotSupportedException e, HttpServletRequest request) + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, + HttpServletRequest request) { String requestURI = request.getRequestURI(); - String msg = String.format("访问的URL[%s]不支持%s请求", requestURI, e.getMethod()); - log.error(msg, e); - return AjaxResult.error(msg); + log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod()); + return AjaxResult.error(e.getMessage()); } /** * 拦截未知的运行时异常 */ @ExceptionHandler(RuntimeException.class) - public AjaxResult notFount(RuntimeException e, HttpServletRequest request) + public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); - String msg = String.format("访问的URL[%s]发生异常%s", requestURI, e.getMessage()); - log.error(msg, e); - return AjaxResult.error(msg); + log.error("请求地址'{}',发生未知异常.", requestURI, e); + return AjaxResult.error(e.getMessage()); } /** * 系统异常 */ @ExceptionHandler(Exception.class) - public AjaxResult handleException(Exception e) + public AjaxResult handleException(Exception e, HttpServletRequest request) { - log.error(e.getMessage(), e); - return AjaxResult.error("服务器错误,请联系管理员"); + String requestURI = request.getRequestURI(); + log.error("请求地址'{}',发生系统异常.", requestURI, e); + return AjaxResult.error(e.getMessage()); } /** * 业务异常 */ - @ExceptionHandler(BusinessException.class) - public Object businessException(HttpServletRequest request, BusinessException e) + @ExceptionHandler(ServiceException.class) + public Object handleServiceException(ServiceException e, HttpServletRequest request) { log.error(e.getMessage(), e); if (ServletUtils.isAjaxRequest(request)) @@ -91,10 +90,7 @@ public class GlobalExceptionHandler } else { - ModelAndView modelAndView = new ModelAndView(); - modelAndView.addObject("errorMessage", e.getMessage()); - modelAndView.setViewName("error/business"); - return modelAndView; + return new ModelAndView("error/service", "errorMessage", e.getMessage()); } } @@ -102,7 +98,7 @@ public class GlobalExceptionHandler * 自定义验证异常 */ @ExceptionHandler(BindException.class) - public AjaxResult validatedBindException(BindException e) + public AjaxResult handleBindException(BindException e) { log.error(e.getMessage(), e); String message = e.getAllErrors().get(0).getDefaultMessage(); @@ -113,7 +109,7 @@ public class GlobalExceptionHandler * 演示模式异常 */ @ExceptionHandler(DemoModeException.class) - public AjaxResult demoModeException(DemoModeException e) + public AjaxResult handleDemoModeException(DemoModeException e) { return AjaxResult.error("演示模式,不允许操作"); } diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenTableServiceImpl.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenTableServiceImpl.java index b809d6a3..c4818965 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenTableServiceImpl.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenTableServiceImpl.java @@ -26,7 +26,7 @@ import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.GenConstants; import com.ruoyi.common.core.text.CharsetKit; import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.exception.BusinessException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.generator.domain.GenTable; import com.ruoyi.generator.domain.GenTableColumn; @@ -181,7 +181,7 @@ public class GenTableServiceImpl implements IGenTableService } catch (Exception e) { - throw new BusinessException("导入失败:" + e.getMessage()); + throw new ServiceException("导入失败:" + e.getMessage()); } } @@ -270,7 +270,7 @@ public class GenTableServiceImpl implements IGenTableService } catch (IOException e) { - throw new BusinessException("渲染模板失败,表名:" + table.getTableName()); + throw new ServiceException("渲染模板失败,表名:" + table.getTableName()); } } } @@ -292,7 +292,7 @@ public class GenTableServiceImpl implements IGenTableService List dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); if (StringUtils.isEmpty(dbTableColumns)) { - throw new BusinessException("同步数据失败,原表结构不存在"); + throw new ServiceException("同步数据失败,原表结构不存在"); } List dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList()); @@ -385,26 +385,26 @@ public class GenTableServiceImpl implements IGenTableService JSONObject paramsObj = JSONObject.parseObject(options); if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) { - throw new BusinessException("树编码字段不能为空"); + throw new ServiceException("树编码字段不能为空"); } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) { - throw new BusinessException("树父编码字段不能为空"); + throw new ServiceException("树父编码字段不能为空"); } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) { - throw new BusinessException("树名称字段不能为空"); + throw new ServiceException("树名称字段不能为空"); } } else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory())) { if (StringUtils.isEmpty(genTable.getSubTableName())) { - throw new BusinessException("关联子表的表名不能为空"); + throw new ServiceException("关联子表的表名不能为空"); } else if (StringUtils.isEmpty(genTable.getSubTableFkName())) { - throw new BusinessException("子表关联的外键名不能为空"); + throw new ServiceException("子表关联的外键名不能为空"); } } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java index 1dc17b76..5a0df39d 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysConfigServiceImpl.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Service; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.exception.BusinessException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.CacheUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.SysConfig; @@ -134,7 +134,7 @@ public class SysConfigServiceImpl implements ISysConfigService SysConfig config = selectConfigById(configId); if (StringUtils.equals(UserConstants.YES, config.getConfigType())) { - throw new BusinessException(String.format("内置参数【%1$s】不能删除 ", config.getConfigKey())); + throw new ServiceException(String.format("内置参数【%1$s】不能删除 ", config.getConfigKey())); } configMapper.deleteConfigById(configId); CacheUtils.remove(getCacheName(), getCacheKey(config.getConfigKey())); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java index 584b5aea..4047f61f 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java @@ -13,7 +13,7 @@ import com.ruoyi.common.core.domain.Ztree; import com.ruoyi.common.core.domain.entity.SysDept; import com.ruoyi.common.core.domain.entity.SysRole; import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.exception.BusinessException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.mapper.SysDeptMapper; import com.ruoyi.system.service.ISysDeptService; @@ -201,7 +201,7 @@ public class SysDeptServiceImpl implements ISysDeptService // 如果父节点不为"正常"状态,则不允许新增子节点 if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) { - throw new BusinessException("部门停用,不允许新增"); + throw new ServiceException("部门停用,不允许新增"); } dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); return deptMapper.insertDept(dept); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java index a04f5640..a4a1e36c 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDictTypeServiceImpl.java @@ -11,7 +11,7 @@ import com.ruoyi.common.core.domain.Ztree; import com.ruoyi.common.core.domain.entity.SysDictData; import com.ruoyi.common.core.domain.entity.SysDictType; import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.exception.BusinessException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.DictUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.mapper.SysDictDataMapper; @@ -126,7 +126,7 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService SysDictType dictType = selectDictTypeById(dictId); if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0) { - throw new BusinessException(String.format("%1$s已分配,不能删除", dictType.getDictName())); + throw new ServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName())); } dictTypeMapper.deleteDictTypeById(dictId); DictUtils.removeDictCache(dictType.getDictType()); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysPostServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysPostServiceImpl.java index 885b8924..a68e9ce4 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysPostServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysPostServiceImpl.java @@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.exception.BusinessException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.SysPost; import com.ruoyi.system.mapper.SysPostMapper; @@ -93,7 +93,7 @@ public class SysPostServiceImpl implements ISysPostService * @throws Exception */ @Override - public int deletePostByIds(String ids) throws BusinessException + public int deletePostByIds(String ids) { Long[] postIds = Convert.toLongArray(ids); for (Long postId : postIds) @@ -101,7 +101,7 @@ public class SysPostServiceImpl implements ISysPostService SysPost post = selectPostById(postId); if (countUserPostById(postId) > 0) { - throw new BusinessException(String.format("%1$s已分配,不能删除", post.getPostName())); + throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName())); } } return postMapper.deletePostByIds(postIds); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java index 0714edc1..dadd2b86 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java @@ -12,7 +12,7 @@ import com.ruoyi.common.annotation.DataScope; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.core.domain.entity.SysRole; import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.exception.BusinessException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.system.domain.SysRoleDept; @@ -160,7 +160,7 @@ public class SysRoleServiceImpl implements ISysRoleService SysRole role = selectRoleById(roleId); if (countUserRoleByRoleId(roleId) > 0) { - throw new BusinessException(String.format("%1$s已分配,不能删除", role.getRoleName())); + throw new ServiceException(String.format("%1$s已分配,不能删除", role.getRoleName())); } } // 删除角色与菜单关联 @@ -314,7 +314,7 @@ public class SysRoleServiceImpl implements ISysRoleService { if (StringUtils.isNotNull(role.getRoleId()) && role.isAdmin()) { - throw new BusinessException("不允许操作超级管理员角色"); + throw new ServiceException("不允许操作超级管理员角色"); } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java index 884b7d0f..110829a6 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java @@ -12,7 +12,7 @@ import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.core.domain.entity.SysRole; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.text.Convert; -import com.ruoyi.common.exception.BusinessException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.security.Md5Utils; import com.ruoyi.system.domain.SysPost; @@ -399,7 +399,7 @@ public class SysUserServiceImpl implements ISysUserService { if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin()) { - throw new BusinessException("不允许操作超级管理员用户"); + throw new ServiceException("不允许操作超级管理员用户"); } } @@ -460,7 +460,7 @@ public class SysUserServiceImpl implements ISysUserService { if (StringUtils.isNull(userList) || userList.size() == 0) { - throw new BusinessException("导入用户数据不能为空!"); + throw new ServiceException("导入用户数据不能为空!"); } int successNum = 0; int failureNum = 0; @@ -505,7 +505,7 @@ public class SysUserServiceImpl implements ISysUserService if (failureNum > 0) { failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); - throw new BusinessException(failureMsg.toString()); + throw new ServiceException(failureMsg.toString()); } else {