package com.uas.report.core.exception;

/**
 * 将失败原因以成功的请求返回
 * 
 * @author sunyj
 * @since 2016年10月19日 上午8:52:17
 */
public class ReportException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	private String message;

	/**
	 * 详细信息
	 */
	private String detailedMessage;

	public ReportException(String message) {
		this.message = message;
	}

	public ReportException(Throwable e) {
		this.message = getMessage(e);
	}

	/**
	 * 获取异常及其Cause拼接成的字符串
	 * 
	 * @param e
	 *            异常
	 * @return 拼接后的结果
	 */
	public String getMessage(Throwable e) {
		StringBuilder sb = new StringBuilder(e.toString());
		if (e.getCause() != null) {
			sb.append("\nCaused by: ").append(getMessage(e.getCause()));
		}
		return sb.toString();
	}

	/**
	 * 获取异常及其Cause的StackTrace拼接成的字符串
	 * 
	 * @param e
	 *            异常
	 * @return 拼接后的结果
	 */
	public String getDetailedMessage(Throwable e) {
		StringBuilder sb = new StringBuilder(e.toString());
		StackTraceElement[] stackTraceElements = e.getStackTrace();
		for (StackTraceElement stackTraceElement : stackTraceElements) {
			sb.append("\n\t").append(stackTraceElement.toString());
		}
		if (e.getCause() != null) {
			sb.append("\nCaused by: ").append(getDetailedMessage(e.getCause()));
		}
		return sb.toString();
	}

	/**
	 * 获取异常及其Cause的StackTrace用以设置详细信息
	 * 
	 * @param e
	 *            异常
	 * @return 本SearchException对象
	 */
	public ReportException setDetailedMessage(Throwable e) {
		this.detailedMessage = getDetailedMessage(e);
		return this;
	}

	public String getMessage() {
		return message;
	}

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

	public String getDetailedMessage() {
		return detailedMessage;
	}

	public void setDetailedMessage(String detailedMessage) {
		this.detailedMessage = detailedMessage;
	}

}
