我对ProductDisplayCmd的自定义实现看起来像这样......
public void performExecute( ) throws ECException {
super.performExecute();
(my code here)
现在,如果产品不可用,则super会抛出ECApplicationException并显示以下消息:
com.ibm.commerce.exception.ECApplicationException:商品 编号“253739”和部件号“9788703055992”无效 目前的合同。
通过启用SEO的网址,我会被重定向到我们的自定义404页面(“对不起,该产品已不再可用。请尝试我们的其中一个替代方案......”)
http://bktestapp01.tm.dom/shop/sbk/bent-isager-nielsen-efterforskerne
使用旧式网址时,由于未处理的异常,我会收到错误页面。
由于我可以捕获异常,我想我可以选择手动重定向到404页面,但这是要走的路吗?特别是:异常类型似乎并没有告诉我到底出了什么问题,所以我可能会意外地从另一种错误中产生404。
答案 0 :(得分:0)
以下是我最终的结果:从超级中捕获异常,然后确定抛出它的原因是产品是否不可用。如果是,则重定向到404页面,否则重新抛出异常。
实现:
public void performExecute( ) throws ECException {
try {
super.performExecute();
} catch (final ECApplicationException e) {
// Let's see if the problem is something that should really be causing a redirect
makeProductHelperAndRedirectTo404IfProductNotAvailable(e);
// If we get here, noting else was thrown
log.error("The reason super.performExecute threw an ECException is unknown and so we can't recover. Re-throwing it.");
throw e;
}
...并在makeProductblablabla方法中:
private ProductDataHelper makeProductHelperAndRedirectTo404IfProductNotAvailable(final ECException cause) throws ECSystemException,
ECApplicationException {
final ProductDataHelper productHelper;
try {
log.trace("Trying to determine if the reason super.performExecute threw an ECException is that the product is unavailable in the store. The execption is attached to this logline.", cause);
productHelper = makeProductHelper(getProductId());
if (productHelper != null) {
if (!productHelper.isActiveInClub()) {
log.trace("Decided that the reason super.performExecute threw an ECException is that the product is unavailable in the store. The execption is attached to this logline. NB! That exception is DISCARDED", cause);
final String pn = productHelper.getISBN();
final ECApplicationException systemException = new ECApplicationException(ECMessage._ERR_PROD_NOT_EXISTING, this.getClass().getName(), "productIsPublished", new Object[]{ pn });
systemException.setErrorTaskName("ProductDisplayErrorView");
throw systemException;
}
}
return productHelper;
} catch (RemoteException e) {
log.error("I was trying to determine if the reason super.performExecute threw an ECException is that the product is unavailable in the store. The original ECException is attached to this logline. NB! That exception is DISCARDED", cause);
throw new ECSystemException(ECMessage._ERR_GENERIC, super.getClass().getName(), "performExecute",ECMessageHelper.generateMsgParms(e.getMessage()), e);
} catch (NamingException e) {
log.error("I was trying to determine if the reason super.performExecute threw an ECException is that the product is unavailable in the store. The original ECException is attached to this logline. NB! That exception is DISCARDED", cause);
throw new ECSystemException(ECMessage._ERR_GENERIC, super.getClass().getName(), "performExecute",ECMessageHelper.generateMsgParms(e.getMessage()), e);
} catch (FinderException e) {
log.error("I was trying to determine if the reason super.performExecute threw an ECException is that the product is unavailable in the store. The original ECException is attached to this logline. NB! That exception is DISCARDED", cause);
throw new ECSystemException(ECMessage._ERR_GENERIC, super.getClass().getName(), "performExecute",ECMessageHelper.generateMsgParms(e.getMessage()), e);
} catch (CreateException e) {
log.error("I was trying to determine if the reason super.performExecute threw an ECException is that the product is unavailable in the store. The original ECException is attached to this logline. NB! That exception is DISCARDED", cause);
throw new ECSystemException(ECMessage._ERR_GENERIC, super.getClass().getName(), "performExecute",ECMessageHelper.generateMsgParms(e.getMessage()), e);
}
}