package com.sonic.common.exception;

import com.sonic.common.rpc.ApiResultCode;

import lombok.Getter;

/**
 * 可预期的业务层面的反馈。
 * 与其说这个是异常，不如说是 message return。
 * 接口有两种返回方式，第一，业务实体，第二，exception message
 * 当业务不满足条件，需要以 message 字符串的形式告诉调用方，这时候就可以通过 BizException 然后全局异常解析的方式来返回
 * @author code
 */
@Getter
public class BizException extends RuntimeException {

    private final String errorCode;

    private final String errorMsg;

    public BizException(ApiResultCode resultCode) {
        super(String.format("BizException{errorCode:%s, errorMsg:%s}", resultCode.getErrorCode(), resultCode.getErrorMsg()));
        this.errorCode = resultCode.getErrorCode();
        this.errorMsg = resultCode.getErrorMsg();
    }

    public BizException(ApiResultCode resultCode, Throwable cause) {
        super(String.format("BizException{errorCode:%s, errorMsg:%s}", resultCode.getErrorCode(), resultCode.getErrorMsg()), cause);
        this.errorCode = resultCode.getErrorCode();
        this.errorMsg = resultCode.getErrorMsg();
    }

    public BizException(String errorCode, String errorMsg) {
        super(String.format("BizException{errorCode:%s, errorMsg:%s}", errorCode, errorMsg));
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public BizException(String errorCode, String errorMsg, Throwable cause) {
        super(String.format("BizException{errorCode:%s, errorMsg:%s}", errorCode, errorMsg), cause);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }
}
