1. commnad里遇到系统级异常(如db related exception),cache住,并且封装成ECSystemException来抛出。
例如:
try {
OrderItemAccessBean[] abOrderItems = getOrderItems(aabOrder);
BigDecimal dOrderProductTotal = BIG_DECIMAL_ZERO;
for(int i = 0; i < abOrderItems.length; i++) {
dOrderProductTotal = (dOrderProductTotal == null ? abOrderItems[i].getTotalProductInEJBType() : dOrderProductTotal.add(abOrderItems[i].getTotalProductInEJBType()));
}
...
}
catch(CreateException e) { throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, getClass().getName(), strMethod, new Object[] { e.toString() }, e); }
catch(FinderException e) { throw new ECSystemException(ECMessage._ERR_FINDER_EXCEPTION, getClass().getName(), strMethod, new Object[] { e.toString() }, e); }
catch(NamingException e) { throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION, getClass().getName(), strMethod, new Object[] { e.toString() }, e); }
catch(RemoteException e) { throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, getClass().getName(), strMethod, new Object[] { e.toString() }, e); }
2. command里遇到应用级别的异常(如参数校验失败,业务逻辑检查不通过),主动抛出ECApplicationException异常。
例如:
try {
String[] strUsageIds = aRequestProperties.getArray(CalculationConstants.EC_USAGE_ID, null);
if(strUsageIds != null) {
inUsageIds = new Integer[strUsageIds.length];
for(int i = 0; i < strUsageIds.length; i++) { inUsageIds[i] = new Integer(strUsageIds[i]); }
}
}
catch(NumberFormatException e) {
throw new ECApplicationException(ECMessage._ERR_BAD_MISSING_CMD_PARAMETER, getClass().getName(), strMethod, new Object[] { CalculationConstants.EC_USAGE_ID });
}
3. 抛出异常时需要提供ECMessage,如果ECMessage里面没有合适的定义好的ECmessage可用,你就需要定义自己的ECMessage,提供resource bundle,message key,message parameters。
4. 抛出异常时需要指定出错处理页面ErrorView,这样commerce上层架构捕获异常以后自动跳转到出错页面,没有指定出错处理页面的,下面默认的出错页面被默认使用。
/**
* Default value of the generic application error task parameter.
*/
public static final String GENERIC_APPLICATION_ERROR_TASK =
"GenericApplicationError";
/**
* Default value of the generic application error task parameter.
*/
public static final String GENERIC_SYSTEM_ERROR_TASK = "GenericSystemError";
5. 出错页面可以通过ErrorDataBean来获取异常中的消息,从而作出适当的异常处理。
一个sample error view的例子(StoreOpenClose.jsp)
<%
String exMsg = "" ;
ErrorDataBean errorBean = new ErrorDataBean();
try {
DataBeanManager.activate (errorBean, request);
String exKey = errorBean.getMessageKey();
//If the message type in the ErrorDataBean is type SYSTEM then
//display the system message. Otherwise the message is type USER
//so display the user message.
if ( errorBean.getECMessage().getType() == ECMessageType.SYSTEM ) {
exMsg = errorBean.getSystemMessage();
} else {
exMsg = errorBean.getMessage();
}
if (exKey.equals( "_ERR_GENERIC" )) {
String[] paramObj = (String[])errorBean.getMessageParam();
exMsg = paramObj[0];
}
} catch (Exception ex) {
exMsg = "" ;
}
%>
...
function initialize() {
if ( " <%= hasIndexFile %> " == "false" ) {
for ( var i=0; i<parent.NAVIGATION.document.DialogForm.elements.length; i++) {
if (parent.NAVIGATION.document.DialogForm.elements[i].name == "opencloseLaunchStoreButton" ) {
parent.NAVIGATION.document.DialogForm.elements[i].disabled = true ;
}
}
}
parent.setContentFrameLoaded( true );
if ( " <%= UIUtil.toJavaScript(exMsg) %> " != "" )
alertDialog( " <%= UIUtil.toJavaScript(exMsg) %> " );
<%
if ( (exMsg.equals( "" )) && (actionPerformed != null ) && (!actionPerformed.equals( "" )) ) {
%>
alertDialog( " <%= UIUtil.toJavaScript((String)opencloseNLS.get( "opencloseSuccess" )) %> " );
<%
}
%>
}