2013-08-21 23 views
0

我一個ProductDisplayCmd的自定義實現看起來像這樣...重定向從CustomProductDisplayCmd至404頁;如果不可用產品

public void performExecute() throws ECException { 
    super.performExecute(); 
    (my code here) 

現在,如果一個產品是不可用的,超拋出這個消息的ECApplicationException:

com.ibm.commerce.exception.ECApplicationException:目錄條目 數字「253739」和零件號碼「9788703055992」對當前合同 無效。

使用支持SEO URL,我重定向到我們自定義的404頁(「哎呀對不起,該產品不再可用。試試我們的夢幻般的選擇之一......」)

http://bktestapp01.tm.dom/shop/sbk/bent-isager-nielsen-efterforskerne

使用舊樣式的網址,我反而會因爲未處理的例外而收到錯誤頁面。

http://bktestapp01.tm.dom/webapp/wcs/stores/servlet/ProductDisplay?langId=100&storeId=10651&catalogId=10013&club=SBK&productId=253739

因爲我可以捕獲該異常,我想我有手動重定向到404頁的選項,不過是要走的路?特別是:異常類型似乎沒有告訴我到底發生了什麼錯誤,所以我可能會意外地使404出現另一種錯誤。

回答

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); 
    } 
}