处理模板评估期间发生的域异常

Handling a domain exception happened during template evaluation

我正在为特定异常类型设计错误页面。

<error-page>
    <exception-type>xxx.AbstractConfigurableNotFoundException</exception-type>
    <location>/xxx/page-not-found.xhtml</location>
</error-page>

我正在使用 OmniFaces 及其异常工厂来处理错误。

<factory>
    <exception-handler-factory>org.omnifaces.exceptionhandler.FullAjaxExceptionHandler</exception-handler-factory>
</factory>

AbstractConfigurable 是一个域 class 并且它的实例正在模板评估期间创建*。 AbstractConfigurable 的构造函数可能会抛出 AbstractConfigurableNotFoundException。当它发生时,此异常将作为原因包装到特定于模板的异常中。

javax.servlet.ServletException
    Caused by: javax.faces.view.facelets.TagAttributeException
         Caused by: com.sun.faces.mgbean.ManagedBeanCreationException
              ...
                   Caused by: xxx.AbstractConfigurableNotFoundException

结果,出现了不同的异常类型,我的 <error-page> 逻辑没有应用。

很明显,我不想处理,javax.faces.view.facelets.TagAttributeException

<error-page>
    <exception-type>javax.faces.view.facelets.TagAttributeException</exception-type>
    <location>/xxx/page-not-found.xhtml</location>
</error-page>

但我想处理任何以 AbstractConfigurableNotFoundException 为原因的异常。

我能做些什么吗?

FullAjaxExceptionHandler 支持上下文参数来设置要解包的异常类型的完全限定名称。以下是其 documentation:

的相关摘录

Configuration

By default only FacesException and ELException are unwrapped. You can supply a context parameter "org.omnifaces.EXCEPTION_TYPES_TO_UNWRAP" to specify additional exception types to unwrap. The context parameter value must be a commaseparated string of fully qualified names of additional exception types. Note that this also covers subclasses of specified exception types.

<context-param>
    <param-name>org.omnifaces.EXCEPTION_TYPES_TO_UNWRAP</param-name>
    <param-value>javax.ejb.EJBException,javax.persistence.RollbackException</param-value>
</context-param>

This context parameter will also be read and used by FacesExceptionFilter.

因此,在您的特定情况下,您可以在 web.xml 中使用以下配置:

<context-param>
    <param-name>org.omnifaces.EXCEPTION_TYPES_TO_UNWRAP</param-name>
    <param-value>com.sun.faces.mgbean.ManagedBeanCreationException</param-value>
</context-param>

请注意,javax.faces.view.facelets.TagAttributeException 是从 FacesException 扩展而来的,默认情况下它已经展开,因此您真的不需要一并指定它。