java使用枚舉封裝錯誤碼及錯誤信息詳解

枚舉封裝錯誤碼及錯誤信息

使用枚舉類型來封裝project中所需要的錯誤碼和錯誤信息,十分方便。

用法如下

package com.dear.simpler.dbservice; 
/**
 * 
 * @author lixiang
 *使用枚舉類型來封裝異常碼和異常信息
 *
 */
public enum DBServiceError {
	 //RPC層調用錯誤碼
	 DB_SERVICE_OK(20100,"服務正常")
	,DB_SERVICE_DBDAO_ERROR(20104,"返回數據庫的具體異常信息")
	
	,DB_SERVICE_SPEAKER_NOT_EXISTED(20108,"數據庫中沒有要查詢的speaker")
	,DB_SERVICE_SPEAKER_HAVE_NOT_VP(20109,"speaker中沒有對應的voiceprint")
	
	,DB_SERVICE_UNKNOWN_ERROR(20101,"未知異常")
	,DB_SERVICE_AGENT_ERROR	(20102,	"DBServiceAgent異常")
	,DB_SERVICE_NETWORK_ERROR (20103,"網絡異常")
	
	,DB_SERVICE_INVALID_FUNCTION(20105,	"方法名不存在")
	,DB_SERVICE_INVALID_PARAMETER(20106,"方法參數錯誤")
	,DB_SERVICE_FUNCTION_NO_ACCESS(20107,"對此方法無訪問權限");
	
    private String msg;
    private int code;    
    private DBServiceError(int code,String msg)
    {
    	this.code=code;
        this.msg=msg;
    }
    
    public String getMsg()
    {
        return this.msg;
    }
    public int getCode() {
      return this.code;
  }	
}

使用時:

DBServiceError.DB_SERVICE_NETWORK_ERROR.getCode();

DBServiceError.DB_SERVICE_NETWORK_ERROR.getMsg();

封裝返回結果類和枚舉錯誤碼

枚舉錯誤碼

/**
 * 枚舉錯誤碼
 */
public emum ErrorCode{ 
    FAILED(601,"操作失敗"),
    AUTH_ERROR(401,"認證失敗"),
    SYS_ERROR(500,"系統錯誤"),
    PARAM_ERROR(400,"參數錯誤"),
    UNKNOWN_ERROR(499,"未知錯誤");
 
    private int code;
    private String message;    
    private ErrorCode(int code, String message) {
     this.code=code;
        this.message=message;
    }    
    public String getMessage() {
        return this.message;
    }
    public int getCode() {
        return this.code;
    }
}

封裝返回結果類

/**
 * 返回結果類
 */
public class JsonResult { 
 private int status = 0; 
 private String msg = "success"; 
 private Object data; 
        // 幾個常用方法
 public JsonResult success() {
            return new JsonResult();
        }
 
        public JsonResult success(Object data) {
            return new JsonResult(data);
        }
 
        public JsonResult error(ErrorCode errorCode) {
            return new JsonResult(errorCode.getCode(), errorCode.getMessage());
        }
 
        public JsonResult error(int state, String message) {
            return new JsonResult(state, message);
        }
 
        public JsonResult error(int state, String message, Object data) {
            return new JsonResult(state, message, data);
        }
 
        // 幾個構造方法
        public JsonResult() {
        }
 
        public JsonResult(Object data) {
            this.data = data;
        }
 
        public JsonResult(Integer state, String message) {
            this.state = state;
            this.message = message;
        }
 
        public JsonResult(Integer state, String message, Object data) {
            this.state = state;
            this.message = message;
            this.data = data;
        }
 
 public Integer getStatus() {
  return status;
 }
 
 public void setStatus(Integer status) {
  this.status = status;
 }
 
 public String getMsg() {
  return msg;
 }
 
 public void setMsg(String msg) {
  this.msg = msg;
 }
 
 public Object getData() {
  return data;
 }
 
 public void setData(Object data) {
  this.data = data;
 }
}

完美!!!

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: