package com.sonic.common.exception;

import com.sonic.common.rpc.ApiResultCode;

import lombok.Getter;

/**
 * 可预期的业务层面的异常。
 * 例如，当第三方组件抛出非运行时异常时，可以转化为 SysException 抛出，由统一异常处理器处理
 * 与 {@link BizException} 的区别是, BizException 负责向调用端返回 message，而 SysException 核心是报告异常，然后向调用端返回 message
 * 具体设计期望，BizException 在全局异常处理时，不会打印异常栈，不会发送邮件告警、短信告警等
 * 而 SysException 则会打印异常栈，且根据配置发出邮件告警或短信告警
 * @author code
 */
@Getter
public class SysException extends RuntimeException {

    private final String errorCode;

    private final String errorMsg;

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

    public SysException(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 SysException(String errorCode, String errorMsg) {
        super(String.format("BizException{errorCode:%s, errorMsg:%s}", errorCode, errorMsg));
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

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