package com.uas.search.exception;

/**
 * 搜索异常
 * 
 * @author sunyj
 * @since 2016年9月30日 下午3:32:48
 */
public class SearchException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	private String message;

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

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

	public SearchException(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 SearchException 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;
	}

}
