Search code examples
jsfomnifaces

Handling a domain exception happened during template evaluation


I'm designing an error page for a particular exception type.

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

I am using OmniFaces and their exception factory to handle errors.

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

AbstractConfigurable is a domain class and its instance is being created during template evaluation*. The constructor of AbstractConfigurable may throw an AbstractConfigurableNotFoundException. When it does, this exception gets wrapped into template-specific exceptions as a cause.

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

As a result, a different exception type comes out and my <error-page> logic isn't applied.

Obviously, I don't want to handle, 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>

but I'd like to handle any exception with an AbstractConfigurableNotFoundException as a cause.

Is there anything I can do about it?


Solution

  • The FullAjaxExceptionHandler supports a context parameter to set fully qualified names of exception types to unwrap. Below is an extract of relevance from its 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.

    So, in your specific case you could use the following configuration in 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>
    

    Note that javax.faces.view.facelets.TagAttributeException extends from FacesException, which is by default already unwrapped, so you don't really need to specify this along.