Exception swallowed in JSF phase class

Splash Forums PrettyFaces Users Exception swallowed in JSF phase class

This topic contains 7 replies, has 3 voices, and was last updated by  Christian Kaltepoth 6 years, 3 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #17955

    rspuler
    Participant

    I have a problem with the use of pretty faces action methods. As I understand they will be executed by a phase listener after (default = after RESTORE_VIEW phase) or before a JSF lifecycle phase.

    The problem is that exceptions are swallowed (and logged) in the com.sun.faces.lifecycle.Phase class (handleAfterPhase/handleBeforePhase). This is pretty bad because I try to load some data in the action method an I can not get to the exception to display it on the frontend.

    Is there any workaround for this issue?

    #21002

    Yeah, I know this problem. I also got into similar situations a while ago. I think PrettyFaces simply rethrows exceptions from action methods and Mojarra swallows the details. Is this correct?

    My workaround was to wrap the action method in a big try-catch block and log all the details there. This way you can find out which exception is thrown and why. You can then catch the specific exception type and display a corresponding message to the user.

    #21003

    I thought we fixed this problem by no longer extending facesexception. Are you using 3.3.0?

    #21004

    rspuler
    Participant

    The exception is logged with level WARNING. So there is no problem for me to find out what the exception is.

    What I would like to do is to handle the exceptions in a general manner higher up the call stack. But because the handleAfterPhase just swallows the exception and returns from the method the exception never gets there.

    I use 3.3.0 but I don’t think the type of exception is a problem as this method catches all exceptions.

    Here is the method from com.sun.faces.lifecycle.Phase.

    protected void handleAfterPhase(FacesContext context,
    ListIterator<PhaseListener> listenersIterator,
    PhaseEvent event) {

    while (listenersIterator.hasPrevious()) {
    PhaseListener listener = listenersIterator.previous();
    if (this.getId().equals(listener.getPhaseId()) ||
    PhaseId.ANY_PHASE.equals(listener.getPhaseId())) {
    try {
    listener.afterPhase(event);
    } catch (Exception e) {
    if (LOGGER.isLoggable(Level.WARNING)) {
    LOGGER.log(Level.WARNING,
    "jsf.lifecycle.phaselistener.exception",
    new Object[]{
    listener.getClass().getName()
    + ".afterPhase()",
    this.getId().toString(),
    ((context.getViewRoot() != null)
    ? context.getViewRoot().getViewId()
    : ""),
    e});
    LOGGER.warning(Util.getStackTraceString(e));
    return;
    }
    }
    }
    }

    }

    #21005

    Ah OK, now I got your point.

    But I think there is not much we can do about this. We currently wrap the exception thrown from the action method with a PrettyException and rethrow that one. That Mojarra is swallowing the exception is IMHO the real problem.

    Or do you have any idea how PrettyFaces should react to something like that instead?

    Christian

    #21006

    rspuler
    Participant

    Yes, it is not really a problem of PrettyFaces but of the JSF implementation.

    I just wanted to find out if someone had run into this problem before and knows a workaround. Maybe it is possible to replace the com.sun.faces.lifecycle.RestoreViewPhase with a custom implementation that overwrites the handleAfterPhase method with better error handling.

    But I am not a JSF expert so I don’t really know how one would do that.

    Thanks anyway for your help

    Rolf

    #21007

    Well, there are actually a few options you can use:

    1. Use Seam Catch – it integrates with JSF and lets you handle exceptions very declaratively (Only an option if you are using CDI or JavaEE 6)

    2. Write your own ExceptionHandlerFactory and add it in faces-config.xml, you can override how JSF handles these exceptions, but it’s not the nicest API, so you might say, “What the $#@!?” Quite a few times as you try to extend it.

    Hope this helps,

    Lincoln

    #21008

    I don’t think it is possible to replace RestoreViewPhase inside Mojarra. But I might be wrong. You should ask this on the Mojarra list.

    Seam Catch would definitively be an option to look at. I doubt that a custom ExceptionHandler will work for you. It doesn’t look like Mojarra is handling the exception thrown from the PhaseListener with an ExceptionHandler (see the code from your second post).

    If you are using CDI you should really take a look at Seam Catch. If you are using Spring, you could try to implement an Interceptor that catches and handles the exceptions.

    Christian

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

Comments are closed.