package net.tooan.ynpay.commons;
import net.tooan.ynpay.third.jfinal.log.Logger;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import javax.ejb.EJBException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ResourceBundle;
/**
* Created with IntelliJ IDEA.
* User: Jing
* Date: 13-10-14
* Time: 下午3:12
*/
public class BusinessException extends EJBException {
private static Logger logger = Logger.getLogger(BusinessException.class);
public BusinessException() {
this(ErrorConstants.unknownErrCode, StringUtils.EMPTY, null);
}
/**
* @param errMessage
*/
public BusinessException(String errMessage) {
this(ErrorConstants.unknownErrCode, errMessage, null);
}
/**
* @param errCode
* @param errMessage
*/
public BusinessException(Integer errCode, String errMessage) {
this(errCode, errMessage, null);
}
/**
* @param errMessage
* @param ex
*/
public BusinessException(String errMessage, Exception ex) {
this(ErrorConstants.unknownErrCode, errMessage, ex);
}
/**
* @param ex
*/
public BusinessException(Exception ex) {
this((ex instanceof BusinessException)
? ((BusinessException) ex).getErrCode()
: ErrorConstants.unknownErrCode, (ex instanceof BusinessException)
? ((BusinessException) ex).getErrMessage()
: ex.getMessage(), ex);
}
/**
* @param errCode
* @param errMessage
* @param ex
*/
public BusinessException(Integer errCode, String errMessage, Exception ex) {
super(String.format("[%s] %s", errCode, errMessage), ex);
this.errCode = errCode;
this.errMessage = errMessage;
}
private Integer errCode;
private String errMessage;
public Integer getErrCode() {
return errCode;
}
public void setErrCode(Integer errCode) {
this.errCode = errCode;
}
public String getErrMessage() {
return errMessage;
}
public void setErrMessage(String errMessage) {
this.errMessage = errMessage;
}
/**
* @param errorCode
* @param errorMessage
* @return
*/
private static BusinessException refineException(int errorCode, String errorMessage) {
if (errorCode == 1461) {
return new BusinessException(1461, "超过字段范围的值");
}
String[] array = StringUtils.split(errorMessage, "[':''\n']");
if (ArrayUtils.isNotEmpty(array) && array.length >= 2) {
return new BusinessException(array[1]);
} else {
return new BusinessException(errorMessage);
}
}
/**
* @param e
* @return
*/
private static BusinessException refineException(SQLException e) {
if (!StringUtils.equals(e.getClass().getName(), SQLException.class.getName())) {
SQLException ex = (SQLException) e.getCause();
if (ex != null) {
return refineException(ex);
}
}
return refineException(e.getErrorCode(), e.getMessage());
}
/**
* @param e
* @return
*/
public static BusinessException parseException(Throwable e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e);
}
if (e instanceof SQLException) {
return refineException((SQLException) e);
}
if (e instanceof BusinessException) {
return (BusinessException) e;
}
Throwable ex = e.getCause();
if (ex instanceof BusinessException) {
return (BusinessException) ex;
}
if (e instanceof Exception) {
return new BusinessException((Exception) e);
}
return new BusinessException(e.getMessage());
}
protected static String getProperty(Class<?> clazz, String ekey, String fix, String defaultValue, Object... args) {
if (StringUtils.isNotBlank(ekey)) {
StackTraceElement[] elements = (new Throwable()).getStackTrace();
for (StackTraceElement ele : elements) {
try {
if (Class.forName(ele.getClassName()).getSuperclass() == clazz) {
String clazzName3 = ele.getClassName();
ResourceBundle bundle = ResourceBundle.getBundle(clazzName3);
String key = String.format("%s.%s", StringUtils.trimToEmpty(ekey), fix);
if (bundle.containsKey(key)) {
return MessageFormat.format(bundle.getString(key), args);
}
break;
}
} catch (ClassNotFoundException e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e);
}
}
}
}
return defaultValue;
}
}