77.安心技术梳理 - 开发中 - 异常- 类和方法的创建和使用,干货

开发中异常的输出是必要的,我们需要统一的数据标准,做个统一工具,仅供参考

1.实际应用的方式

(1)创建常量返回异常结果

import com.Result;
Result.failResult(CocoonErrorCode.PURCHASECARD_ACT_EXCEPTION, "付费卡活动异常")

(2)创建异常类返回

throw BusinessException.of(CocoonErrorCode.PAIDCOUPON_NOEXIT).tips("查询列表为空");

2.实现的工具类

(1)异常分类

public interface CocoonErrorCodePrefix {
    //通用
    String COMMON = "30";
}

(2)创建异常常量

import com.ErrorCode;
public class CocoonErrorCode extends ErrorCode {
    public CocoonErrorCode(String code, String message) {
        super(code, message);
    }
    @Override
    protected String getPrefix() {
        return CocoonErrorCodePrefix.COMMON;
    }
    public static final CocoonErrorCode NOEXIT = new CocoonErrorCode("50001", "没有活动");
    public static final CocoonErrorCode FRESHDATA_NOEXIT = new CocoonErrorCode("50002", "活动失效");

}

(3)创建异常类

import com.Result;
import java.io.Serializable;
import java.util.Objects;
public abstract class ErrorCode implements Serializable {
    //错误码
    private String code;
    //错误信息
    private String message;
    public ErrorCode(String code, String message) {
        this.code = code;
        this.message = message;
    }
    //错误码前缀
    protected abstract String getPrefix();
    public Result result() {
        return Result.failResult(this);
    }
    public Result result(String tips) {
        return Result.failResult(this, tips);
    }
    public String getCode() {
        return getPrefix() + code;
    }
    public String getMessage() {
        return message;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ErrorCode errorCode = (ErrorCode) o;
        return Objects.equals(code, errorCode.code);
    }
    @Override
    public int hashCode() {
        return Objects.hash(code);
    }
}

(4)返回异常方法,创建异常类

import com.ErrorCode;
import com.Result;
public class BusinessException extends RuntimeException {
    //异常码信息
    private ErrorCode errorCode;
    //提示信息
    private String tips;
    public BusinessException() {}
    public static BusinessException of(ErrorCode errorCode) {
        return new BusinessException(errorCode);
    }
    public static BusinessException of(ErrorCode errorCode, String tips) {
        return new BusinessException(errorCode, tips);
    }
    public BusinessException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
    }
    public BusinessException(ErrorCode errorCode, String tips) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
        this.tips = tips;
    }
    public BusinessException(ErrorCode errorCode, Throwable cause) {
        super(errorCode.getMessage(), cause);
        this.errorCode = errorCode;
    }
    public BusinessException tips(String tips) {
        this.tips = tips;
        return this;
    }
    public Result result() {
        return errorCode.result(tips);
    }
    public ErrorCode getErrorCode() {
        return errorCode;
    }
    public void setErrorCode(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }
    public String getTips() {
        return tips;
    }
}

你可能感兴趣的:(安心技术)