创建CustomException类代码如下
import org.springframework.http.HttpStatus; public class CustomException extends Exception { private int pbsCode; private String pbsMessage; private HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; public HttpStatus getHttpStatus() { return httpStatus; } public CustomException() { super(); } public CustomException(int pbsCode,String pbsMessage,HttpStatus httpStatus) { super(pbsMessage); this.pbsCode = pbsCode; this.pbsMessage = pbsMessage; this.httpStatus = httpStatus; } public CustomException(int pbsCode,String pbsMessage) { super(pbsMessage); this.pbsCode = pbsCode; this.pbsMessage = pbsMessage; } public int getPbsCode() { return pbsCode; } public String getPbsMessage() { return pbsMessage; } public void setHttpStatus(HttpStatus httpStatus) { this.httpStatus = httpStatus; } }
创建CustomExceptionBuilder继承CustomException
代码如下
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; /** * <p>Title: CustomExceptionBuilder</p> * <p>Description: 自定义错误建造者</p> * * @author kinglo * @version 1.0.0 * @date 2018/6/7 9:10 */ @Component public class CustomExceptionBuilder extends Exception { @Autowired private Environment env; public CustomException build(int code,HttpStatus httpStatus){ String msg = env.getProperty((code+"")); return new CustomException(code,msg,httpStatus); } public CustomException build(int code){ String msg = env.getProperty((code+"")); return new CustomException(code,msg); } }
创建ErrorMessageConstant 里面定义的是异常code常量 可自定义code常量
代码如下
import org.springframework.context.annotation.Configuration; /** * <p>Title: ErrorMessageConstant</p> * <p>Description: 错误代码配置类</p> * * @author kinglo * @version 1.0.0 * @date 2018/6/7 9:05 */ @Configuration public class ErrorMessageConstant { /** * 刷新Token失败! */ public static final int ERROR_CODE_50001 = 50001; /** * 系统用户已存在! */ public static final int ERROR_CODE_10002 = 10002; /** * 找不到系统用户! */ public static final int ERROR_CODE_10001 = 10001; /** * 图片上传失败! */ public static final int ERROR_CODE_10004 = 10004; /** * 钉钉免登陆失败! */ public static final int ERROR_CODE_10003 = 10003; /** * 用户名或密码错误! */ public static final int ERROR_CODE_10000 = 10000; /** * 根据用户获取菜单失败 */ public static final int ERROR_CODE_10100 = 10100; /** * 新增菜单失败 */ public static final int ERROR_CODE_10101 = 10101; /** * 新增菜单参数为null */ public static final int ERROR_CODE_10102 = 10102; /** * 编辑菜单失败 */ public static final int ERROR_CODE_10103 = 10103; /** * 编辑菜单是主键为空 */ public static final int ERROR_CODE_10104 = 10104; /** * 删除菜单失败 */ public static final int ERROR_CODE_10105 = 10105; /** * 根据主键查找角色菜单失败 */ public static final int ERROR_CODE_10110 = 10110; /** * 根据角色id查找角色菜单失败 */ public static final int ERROR_CODE_10111 = 10111; /** * 新增角色菜单失败 */ public static final int ERROR_CODE_10112 = 10112; /** * 更新角色菜单失败 */ public static final int ERROR_CODE_10113 = 10113; /** * 根据主键删除角色菜单失败 */ public static final int ERROR_CODE_10114 = 10114; /** * 用户权限TOKEN错误 */ public static final int ERROR_CODE_40000 = 40000; /** * 用户权限不足 */ public static final int ERROR_CODE_40001 = 40001; /** * 角色名称重复 */ public static final int ERROR_CODE_20000 = 20000; /** * 角色代码重复 */ public static final int ERROR_CODE_20001 = 20001; /** * 用户删除失败 */ public static final int ERROR_CODE_21001 = 21001; /** * 用户状态更新失败 */ public static final int ERROR_CODE_21002 = 21002; /** * 上传文件为空 */ public static final int ERROR_CODE_30001 = 30001; /** * 上传文件格式不支持 */ public static final int ERROR_CODE_30002 = 30002; /** * 会议保存失败 */ public static final int ERROR_CODE_31001 = 31001; /** * 会议删除失败 */ public static final int ERROR_CODE_31002 = 31002; /** * 参会人员添加失败 */ public static final int ERROR_CODE_32001 = 32001; /** * 参会人员删除失败 */ public static final int ERROR_CODE_32002 = 32002; /** * 参会人员签到失败 */ public static final int ERROR_CODE_32003 = 32003; /** * 会议点赞失败 */ public static final int ERROR_CODE_33001 = 33001; /** * 会议取消点赞失败 */ public static final int ERROR_CODE_33002 = 33002; /** * 会议评论失败 */ public static final int ERROR_CODE_34001 = 34001; /** * 会议评论删除失败 */ public static final int ERROR_CODE_34002 = 34002; /** * 题库目录保存失败 */ public static final int ERROR_CODE_35000 = 35000; /** * 题库目录删除失败 */ public static final int ERROR_CODE_35001 = 35001; /** * 题目信息保存失败 */ public static final int ERROR_CODE_35002 = 35002; /** * 题目信息删除失败 */ public static final int ERROR_CODE_35003 = 35003; /** * 题目选项保存失败 */ public static final int ERROR_CODE_35004 = 35004; /** * 题目选项删除失败 */ public static final int ERROR_CODE_35005 = 35005; /** * 试卷保存失败 */ public static final int ERROR_CODE_35006 = 35006; /** * 试卷删除失败 */ public static final int ERROR_CODE_35007 = 35007; /** * 试卷题目关联失败 */ public static final int ERROR_CODE_35008 = 35008; /** * 试卷题目移除失败 */ public static final int ERROR_CODE_35009 = 35009; /** * 参数有误,请稍后重试! */ public static final int ERROR_CODE_80000 = 80000; /** * 数据存在关联,无法删除! */ public static final int ERROR_CODE_80001 = 80001; /** * 当前日期每一一言已存在,不可重复添加 */ public static final int ERROR_CODE_80002 = 80002; /** * 根据主键查询数据字典信息失败 */ public static final int ERROR_CODE_60000 = 60000; /** * 获取所有数据字典信息失败 */ public static final int ERROR_CODE_60001 = 60001; /** * 分页获取数据字典信息失败 */ public static final int ERROR_CODE_60002 = 60002; /** * 新增数据字典信息失败 */ public static final int ERROR_CODE_60003 = 60003; /** * 更新数据字典信息失败 */ public static final int ERROR_CODE_60004 = 60004; /** * 删除数据字典信息失败 */ public static final int ERROR_CODE_60005 = 60005; /** * 查询所有字典明细信息失败 */ public static final int ERROR_CODE_60006 = 60006; /** * 更新数据字典明细失败 */ public static final int ERROR_CODE_60007 = 60007; /** * 增加数据字典明细失败 */ public static final int ERROR_CODE_60008 = 60008; /** * 删除条数据字典明细失败 */ public static final int ERROR_CODE_60009 = 60009; /** * 查询组织机构失败 */ public static final int ERROR_CODE_60010 = 60010; /** * 修改组织机构失败 */ public static final int ERROR_CODE_60011 = 60011; /** * 根据类型查看组织机构失败 */ public static final int ERROR_CODE_60012 = 60012; /** * 模糊查询组织机构失败 */ public static final int ERROR_CODE_60013 = 60013; /** * 查询党员资料异常 */ public static final int ERROR_CODE_70001 = 70001; /** * 更新党员资料异常 */ public static final int ERROR_CODE_70002 = 70002; /** * 新增党员资料异常 */ public static final int ERROR_CODE_70003 = 70003; /** * 导出党员资料时异常 */ public static final int ERROR_CODE_70004 = 70004; /** * 党员倾诉问题更新失败 */ public static final int ERROR_CODE_44000 = 44000; /** * 党员倾诉问题删除失败 */ public static final int ERROR_CODE_44001 = 44001; /** * 党员倾诉问题明细添加失败 */ public static final int ERROR_CODE_44100 = 44100; /** * 党员倾诉问题明细删除失败 */ public static final int ERROR_CODE_44200 = 44200; /** * 发布党建圈失败 */ public static final int ERROR_CODE_61001 = 61001; /** * 每日签到插入失败 */ public static final int ERROR_CODE_800 = 800; /** * 每日签到删除失败 */ public static final int ERROR_CODE_801 = 801; /** * 每日签到更新失败 */ public static final int ERROR_CODE_802 = 802; /** * 每日签到查询数据失败 */ public static final int ERROR_CODE_803 = 803; /** * 没有每日签到记录数据 */ public static final int ERROR_CODE_804 = 804; /** * 资源不存在 */ public static final int RESOURCES_DO_NOT_EXIST = 404; }