package com.hotent.base.exception;

import org.apache.commons.lang.exception.ExceptionUtils;

import com.hotent.base.enums.ResponseErrorEnums;

import dm.jdbc.util.StringUtil;

/**
 * 通用异常
 *
 * @company 广州宏天软件股份有限公司
 * @author heyifan
 * @email heyf@jee-soft.cn
 * @date 2020年4月4日
 */
public class BaseException extends RuntimeException{
	private static final long serialVersionUID = 1L;
	/**
	 * 错误代码
	 */
	protected String code;
	/**
	 * 返回的异常信息
	 * <pre>
	 * 通过restful接口返回到前端的信息
	 * </pre>
	 */
	protected String message;
	/**
	 * 打印的异常信息
	 * <pre>
	 * 通过控制台或者输出到统一日志平台的信息
	 * </pre>
	 */
	//protected String detailMessage;
	
	protected ResponseErrorEnums enums;
	
	public BaseException() {
		super();
	}
	
	public BaseException(Throwable cause) {
		super(cause);
		this.message = ExceptionUtils.getRootCauseMessage(cause);
	}
	
	public BaseException(String message) {
		super(message);
		this.message = message;
	}
	
	public BaseException(String message, Throwable cause) {
		super(message, cause);
		String rootMessage = ExceptionUtils.getRootCauseMessage(cause);
		String finalMessage = StringUtil.isNotEmpty(rootMessage) ? rootMessage : message;
		this.message = finalMessage;
	}
	
	public BaseException(ResponseErrorEnums enums) {
		super();
		this.code = enums.getCode();
		this.message = enums.getMessage();
		this.enums = enums;
	}

	public BaseException(ResponseErrorEnums enums, String message) {
		super();
		this.code = enums.getCode();
		this.message = message;
		this.enums = enums;
	}

	public BaseException(ResponseErrorEnums enums, Throwable throwable) {
		super(throwable);
		this.code = enums.getCode();
		this.message = ExceptionUtils.getRootCauseMessage(throwable);
	}

	public BaseException(ResponseErrorEnums enums, String message, Throwable throwable) {
		super(throwable);
		this.code = enums.getCode();
		this.message = message;
	}

	public String getCode() {
		return code;
	}

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

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public ResponseErrorEnums getResponseErrorEnums() {
		return this.enums;
	}
	
	@Override
	public synchronized Throwable fillInStackTrace() {
		return this;
	}
}
