package com.leanwo.activiti.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 获取异常消息的通用处理类
 * @author YangZhiJie
 * @version 20171213 1.0
 *
 */
public class ExceptionUtil {
	
	private static Logger logger = LoggerFactory.getLogger(ExceptionUtil.class);
	
	/**
	 * 获取异常的消息
	 * @param message
	 * @param exception
	 * @return
	 */
	public static String getMessage(String message, Exception exception) {
		String errMsg = exception.getMessage();
		if(errMsg == null) {
			return message;
		}else {
			return message + errMsg;
		}
	}
	
	/**
	 * Java获取异常的堆栈信息到String的方式：
	 * 
	 * @param t
	 * @return
	 */
	public static String getStackTrace(Throwable e) {
		StringBuilder sb = new StringBuilder();
		StackTraceElement[] elements = e.getStackTrace();
		if(elements == null)
			return null;
		for(StackTraceElement element: elements){
			sb.append(element.toString()).append("\n");
		}
		return sb.toString();
	}
}
