【问题标题】:Redirect from CustomProductDisplayCmd to 404 page if unavailable product如果产品不可用,则从 CustomProductDisplayCmd 重定向到 404 页面
【发布时间】:2023-04-09 02:45:01
【问题描述】:

我的 ProductDisplayCmd 自定义实现如下所示...

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

使用旧式 URL,由于未捕获的异常,我得到一个错误页面。

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

既然我可以捕捉到异常,我想我可以选择手动重定向到 404 页面,但这是要走的路吗?特别是:异常类型似乎并不能准确地告诉我出了什么问题,所以我可能会不小心把另一种错误弄成 404。

【问题讨论】:

    标签: websphere-commerce


    【解决方案1】:

    这是我最终得到的结果:从 super 中捕获异常,然后确定抛出异常的原因是否是产品不可用。如果是,则重定向到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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      相关资源
      最近更新 更多