异常及结果、枚举封装

异常的封装

以全局异常处理
作为错误的状况下的返回值
必须带有{code:"",msg:""}

同时结果集类必须包含:
{code:"",msg:""}

1、结果集封装

public class Result<T> implements Serializable{

    private static final long serialVersionUID = -20552077545148552L;

    private String code;
    private String message;
    private T data;

    public Result() {}

    public Result(CodeMsgEnum codeMsgEnum) {
        this.code = codeMsgEnum.getCode();
        this.message = codeMsgEnum.getMessage();
    }

    public Result(CodeMsgEnum codeMsgEnum, T data) {
        this.code = codeMsgEnum.getCode();
        this.message = codeMsgEnum.getMessage();
        this.data = data;
    }

    public Result(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        return new Result(CodeMsgEnum.SUCCESS, data);
    }

    public static <T> Result<T> success() {
        return new Result(CodeMsgEnum.SUCCESS,null);
    }


    public static  <T> Result<T> error(CodeMsgEnum codeMsgEnum) {
        return new Result(codeMsgEnum,null);
    }

    public static  <T> Result<T> error(String code, String msg) {
        return new Result(code, msg,null);
    }

    public static  <T> Result<T> error(String msg) {
        return new Result(CodeMsgEnum.SREVER_ERROR.getCode(), msg ,null);
    }  
}

2、异常类封装

public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 5858874149529785542L;

    /**
     * code
     */
    private String code;
    /**
     * 异常信息
     */
    private String message;


    public BusinessException() {
    }

    /**
     * 服务端未知异常
     *
     * @param message
     */
    public BusinessException(String message) {
        super(message);
        this.code = CodeMsgEnum.SREVER_ERROR.getCode();
        this.message = message;
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        this.code = CodeMsgEnum.SREVER_ERROR.getCode();
        this.message = message;
    }

    /**
     * 返回常用异常类型:
     *    CodeMsgEnum 通用异常
     * @param codeMsgEnum
     */
    public BusinessException(CodeMsgEnum codeMsgEnum) {
        super(codeMsgEnum.getMessage());
        this.code = codeMsgEnum.getCode();
        this.message = codeMsgEnum.getMessage();
    }

    /**
     * 异常信息传入:
     *    方便每个业务系统定义各自内部异常
     * @param code
     * @param message
     */
    public BusinessException(String code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

}

3、通用异常枚举类

public enum  CodeMsgEnum {

    /**
     * 通用枚举,各个系统模块可
       单独编写异常类
     */
    SUCCESS("200", "成功"),
    SREVER_ERROR("500", "服务器内部错误"),
    UNKNOWN_ERROR("1000000", "未知错误"),
    NO_FIND_REQUEST("1001404","找不到资源"),
    NO_AUTHORIZE("1001401", "未授权"),
    AUTHENTICATION_FAIL("1001403", "认证失败"),
    PARAMS_ERROR("1001400","请求参数不合法"),

   
    private final String code;
    private final String message;

    CodeMsgEnum(String code, String message) {
        this.code = code;
        this.message = message;
    }
    public static CodeMsgEnum getCodeMsgEnumByCode(String code) {
        for (CodeMsgEnum codeMsgEnum : CodeMsgEnum.values()) {
            if (codeMsgEnum.code.equals(code)) {
                return codeMsgEnum;
            }
        }
        return null;
    }
    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

}

你可能感兴趣的:(系统设计)