package com.zcxk.core.common.exception;

import com.zcxk.core.common.support.RespCode;
import lombok.Getter;

import java.text.MessageFormat;

/**
 * @author Andy
 * @description: 全局自定义异常
 * @date 2021/7/16
 * @version V1.0
 **/
@Getter
public class BusinessException extends RuntimeException {

    private int code;

    private String msg;

    private static final long serialVersionUID = 1L;

    public static BusinessException builder(int code, String message) {
        return new BusinessException(code, message);
    }

    public static BusinessException builder(RespCode respCode) {
        return new BusinessException(respCode.getStatus(), respCode.getMessage());
    }


    public static BusinessException builder(String msg) {
        return new BusinessException(msg);
    }

    public static BusinessException builder(String msg, Throwable e) {
        return new BusinessException(msg, e);
    }

    public static BusinessException builder(int code, String msg, Throwable e) {
        return new BusinessException(code, msg, e);
    }

    private BusinessException(String msg) {
        super(msg);
        this.msg = msg;
    }

    private BusinessException(int code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    private BusinessException(String msg, Throwable e) {
        super(msg, e);
        this.msg = msg;
    }

    private BusinessException(int code, String msg, Throwable e) {
        super(msg, e);
        this.code = code;
        this.msg = msg;
    }

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }

    @Override
    public String toString() {
        return MessageFormat.format("BusinessException {0} -- {1}", this.code, this.msg);
    }
}
