package com.tzld.piaoquan.api.common.exception;

import com.tzld.piaoquan.api.common.enums.ExceptionEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 通用异常
 */
public class CommonException extends RuntimeException {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private static final Logger LOGGER = LoggerFactory.getLogger(CommonException.class);
    /**
     * 异常
     */
    private ExceptionEnum exceptionEnum;
    /**
     * 错误码
     */
    private int code;
    /**
     * 异常信息
     */
    private String msg;


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public CommonException(Throwable throwable) {
        super(throwable);
    }

    public CommonException(int code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public CommonException(ExceptionEnum exceptionEnum) {
        super(exceptionEnum.getMsg());
        this.exceptionEnum = exceptionEnum;
        this.code = exceptionEnum.getCode();
        this.msg = exceptionEnum.getMsg();
    }


    public CommonException(int code, String msg, Throwable throwable) {
        super(msg, throwable);
        this.code = code;
        this.msg = msg;
    }

    public CommonException(ExceptionEnum exceptionEnum, Throwable throwable) {
        super(exceptionEnum.getMsg(), throwable);
        this.exceptionEnum = exceptionEnum;
        this.code = exceptionEnum.getCode();
        this.msg = exceptionEnum.getMsg();
    }


    @Override
    public void printStackTrace() {
        if (exceptionEnum != null) {
            LOGGER.info("exception code = {}, msg = {}", exceptionEnum.getCode(), exceptionEnum.getMsg());
        }
        LOGGER.info("exception code = {}, msg = {}", code, msg);
    }
}
