自定义异常处理以及java中的枚举类型

枚举类型是Java 5中新增特性的一部分,它是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁性、安全性以及便捷性。下面先来看看什么是枚举?如何定义枚举?

 

package com.hainei.common.enums;

import sun.awt.SunHints;

public enum BasePermissionType {
    MENU((byte)0,"菜单"),
    BUTTON((byte)1,"按钮");
    public Byte type;
    public String value;

    BasePermissionType(byte type, String value) {
        this.type = type;
        this.value = value;
    }
}

 

自定义异常处理

LdarException

package com.hainei.common.exception;

import com.hainei.common.exception.code.LdarResponseCode;

/**
 * Created with IntelliJ IDEA.
 * User:wq
 * Date:2020/4/10
 * Time: 9:51
 * Description: No Description
 */
public class LdarException extends RuntimeException {

    private final int code;

    private final String deafultMessage;

    public LdarException( int code, String deafultMessage) {
        super(deafultMessage);
        this.code = code;
        this.deafultMessage = deafultMessage;
    }

    public LdarException(LdarResponseCode ldarResponseCode){
        this(ldarResponseCode.getCode(),ldarResponseCode.getMsg());
    }

    public int getCode() {
        return code;
    }

    public String getDeafultMessage() {
        return deafultMessage;
    }
}
View Code

 

以及枚举类型

LdarResponseCode

package com.hainei.common.exception.code;

/**
 * Created with IntelliJ IDEA.
 * User:wq
 * Date:2020/4/10
 * Time: 9:23
 * Description: No Description
 */
public enum LdarResponseCode implements  ResponseCodeInterface{

    /**
     * 这个要和前段约定好
     *code=0:服务器已成功处理了请求。 通常,这表示服务器提供了请求的网页。
     *code=40100001:(授权异常) 请求要求身份验证。 客户端需要跳转到登录页面重新登录
     *code=40100002:(凭证过期) 客户端请求刷新凭证接口
     *code=40300001:没有权限禁止访问
     *code=400xxxxx:系统主动抛出的业务异常
     *code=50000001:系统异常
     *
     */
    SUCCESS(0, "操作成功"),
    DATA_ERROR(40000001,"关键字段为空"),
    ADD_ERROR(40000002,"请先添加设备!"),
    //METHOD_IDENTITY_ERROR(40000002,"数据校验异常"),
    FILE_UPLOAD_ERROR(40000003,"文件上传失败"),

    SYSTEM_ERROR(50000001,"系统异常请稍后再试"),

    ACCOUNT_ERROR(40000003,"该账号不存在"),
    ACCOUNT_PASSWORD_ERROR(40000004,"用户名密码不匹配"),

    OPERATION_ERROR(40000005,"操作失败"),
    OPERATION_DATA_MISS_ERROR(40000006,"数据不存在,请刷新后重试"),
    OPERATION_CATEGORY_HAS_CHILD_ERROR(40000007, "该分类下面还有子节点不能删除"),
    OPERATION_CATEGORY_HAS_DATA_ERROR(40000008, "该分类下面含有数据不能删除"),
    OPERATION_SYS_CONFIG_MISS_DATA_ERROR(40000009, "系统配置数据缺失,请联系管理员"),

    OPERATION_SCHEDULE_GET_CronTrigger_ERROR(40000010, "获取定时任务CronTrigger出现异常"),
    OPERATION_SCHEDULE_CREATE_ERROR(40000010, "创建定时任务失败"),
    OPERATION_SCHEDULE_RUN_ERROR(40000010, "立即执行定时任务失败"),
    OPERATION_SCHEDULE_PAUSE_ERROR(40000010, "暂停定时任务失败"),
    OPERATION_SCHEDULE_RESUME_ERROR(40000010, "恢复定时任务失败"),
    OPERATION_SCHEDULE_DELETE_ERROR(40000010, "删除定时任务失败"),
    OPERATION_SCHEDULE_UPDATE_ERROR(40000010, "更新定时任务失败"),

    OPERATION_DELETE_PERMISSION_ERROR(40000011, "该权限绑定着菜单,无法删除"),
    OPERATION_HAS_SAME_NAME_ERROR(40000012, "已经存在同名数据,请检查后重试"),

    ACTIVATION_CODE_EXPIRED_ERROR(40100001,"系统激活码已失效,请联系管理员"),
    TOKEN_NOT_NULL(40100001,"token 不能为空"),
    SHIRO_AUTHENTICATION_ERROR(40100001,"用户认证异常"),
    ACCOUNT_LOCK(40100001,"该账号被锁定,请联系系统管理员"),
    ACCOUNT_HAS_DELETED_ERROR(40100001,"该账号已被删除,请联系系统管理员"),
    TOKEN_ERROR(40100001,"用户未登录,请重新登录");


    /**
     *  响应码
     */
    private int code;

    /**
     * 提示
     */
    private String msg;

    LdarResponseCode(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }
}
View Code

 

ResponseCodeInterface接口

package com.hainei.common.exception.code;

public interface ResponseCodeInterface {
    int getCode();

    String getMsg();
}

 

posted @ 2020-03-20 16:50  wq9  阅读(408)  评论(0编辑  收藏  举报