ApiException.java 1.04 KB
package cn.csbr.app.exception;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 自定义的业务异常--需要捕获
 *
 * @author lid
 */
public class ApiException extends Exception {
    private String errorMsg;

    public ApiException() {
        super("发生错误!");
        this.errorMsg = "发生错误!";//默认信息
    }
    public ApiException(String errorMsg) {
        super("发生错误!");
        this.errorMsg = errorMsg;//默认信息
    }
    public <T> ApiException(String msg, Class<T> logObj) {
        super(msg);
        setErrorMsg(msg);
        // 记录日志
        Logger logger = LogManager.getLogger(logObj);
        logger.error(msg);
    }

    @Override
    public String getMessage() {
        return super.getMessage();
    }

    @Override
    public String toString() {
        return super.toString();
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

}