You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2011/06/30 04:21:01 UTC

svn commit: r1141363 - /myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/context/AjaxExceptionHandlerImpl.java

Author: lu4242
Date: Thu Jun 30 02:21:00 2011
New Revision: 1141363

URL: http://svn.apache.org/viewvc?rev=1141363&view=rev
Log:
MYFACES-3053 Improve error reporting and logging (Fix Ajax Case)

Modified:
    myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/context/AjaxExceptionHandlerImpl.java

Modified: myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/context/AjaxExceptionHandlerImpl.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/context/AjaxExceptionHandlerImpl.java?rev=1141363&r1=1141362&r2=1141363&view=diff
==============================================================================
--- myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/context/AjaxExceptionHandlerImpl.java (original)
+++ myfaces/shared/trunk_4.0.x/core/src/main/java/org/apache/myfaces/shared/context/AjaxExceptionHandlerImpl.java Thu Jun 30 02:21:00 2011
@@ -19,8 +19,10 @@
 package org.apache.myfaces.shared.context;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Queue;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -142,109 +144,128 @@ public class AjaxExceptionHandlerImpl ex
                 handled = new LinkedList<ExceptionQueuedEvent>();
             }
             
-            boolean init = false;
-            PartialResponseWriter partialWriter = null;
-
-            try
+            List<Throwable> throwableList = new ArrayList<Throwable>();
+            FacesContext facesContext = null;
+            
+            do
             {
-                do
+                // For each ExceptionEvent in the list
+                
+                // get the event to handle
+                ExceptionQueuedEvent event = unhandled.peek();
+                try
                 {
-                    // For each ExceptionEvent in the list
+                    // call its getContext() method
+                    ExceptionQueuedEventContext context = event.getContext();
+
+                    if (facesContext == null)
+                    {
+                        facesContext = event.getContext().getContext();
+                    }
                     
-                    // get the event to handle
-                    ExceptionQueuedEvent event = unhandled.peek();
-                    try
+                    // and call getException() on the returned result
+                    Throwable exception = context.getException();
+                    
+                    // Upon encountering the first such Exception that is not an instance of
+                    // javax.faces.event.AbortProcessingException
+                    if (!shouldSkip(exception))
                     {
-                        // call its getContext() method
-                        ExceptionQueuedEventContext context = event.getContext();
-    
-                        if (!init)
-                        {
-                            //Retrieve the partial writer used to render the exception
-                            FacesContext facesContext = event.getContext().getContext();
-                            facesContext = (facesContext == null) ? FacesContext.getCurrentInstance() : facesContext;
-                            ExternalContext externalContext = facesContext.getExternalContext();
-                            
-                            //Clean up the response if by some reason something has been written before.
-                            if (!facesContext.getExternalContext().isResponseCommitted())
-                            {
-                                facesContext.getExternalContext().responseReset();
-                            }
-                            
-                            PartialViewContext partialViewContext = facesContext.getPartialViewContext();
-                            partialWriter = partialViewContext.getPartialResponseWriter();
-        
-                            // ajax request --> xml error page 
-                            externalContext.setResponseContentType("text/xml");
-                            externalContext.setResponseCharacterEncoding("UTF-8");
-                            externalContext.addResponseHeader("Cache-control", "no-cache");
-                            partialWriter.startDocument();
-                            init = true;
-                        }
+                        // set handledAndThrown so that getHandledExceptionQueuedEvent() returns this event
+                        handledAndThrown = event;
                         
-                        // and call getException() on the returned result
-                        Throwable exception = context.getException();
+                        Throwable rootCause = getRootCause(exception);
                         
-                        // Upon encountering the first such Exception that is not an instance of
-                        // javax.faces.event.AbortProcessingException
-                        if (!shouldSkip(exception))
-                        {
-                            // set handledAndThrown so that getHandledExceptionQueuedEvent() returns this event
-                            handledAndThrown = event;
-                            
-                            Throwable rootCause = getRootCause(exception);
-                            
-                            renderAjaxError(event, partialWriter, rootCause == null ? exception : rootCause);
-                            
-                            //break;
-                        }
-                        else
-                        {
-                            // Testing mojarra it logs a message and the exception
-                            // however, this behaviour is not mentioned in the spec
-                            log.log(Level.SEVERE, exception.getClass().getName() + " occured while processing " +
-                                    (context.inBeforePhase() ? "beforePhase() of " : 
-                                            (context.inAfterPhase() ? "afterPhase() of " : "")) + 
-                                    "phase " + context.getPhaseId() + ": " +
-                                    "UIComponent-ClientId=" + 
-                                    (context.getComponent() != null ? 
-                                            context.getComponent().getClientId(context.getContext()) : "") + ", " +
-                                    "Message=" + exception.getMessage());
-                            
-                            log.log(Level.SEVERE, exception.getMessage(), exception);
-                        }
+                        throwableList.add(rootCause == null ? exception : rootCause);
+                        
+                        //break;
                     }
-                    finally
+                    else
                     {
-                        // if we will throw the Exception or if we just logged it,
-                        // we handled it in either way --> add to handled
-                        handled.add(event);
-                        unhandled.remove(event);
+                        // Testing mojarra it logs a message and the exception
+                        // however, this behaviour is not mentioned in the spec
+                        log.log(Level.SEVERE, exception.getClass().getName() + " occured while processing " +
+                                (context.inBeforePhase() ? "beforePhase() of " : 
+                                        (context.inAfterPhase() ? "afterPhase() of " : "")) + 
+                                "phase " + context.getPhaseId() + ": " +
+                                "UIComponent-ClientId=" + 
+                                (context.getComponent() != null ? 
+                                        context.getComponent().getClientId(context.getContext()) : "") + ", " +
+                                "Message=" + exception.getMessage());
+                        
+                        log.log(Level.SEVERE, exception.getMessage(), exception);
                     }
-                } while (!unhandled.isEmpty());
-            }
-            catch (IOException ioe)
-            {
-                throw new FacesException("Could not write the error page", ioe);
-            }
-            finally
+                }
+                finally
+                {
+                    // if we will throw the Exception or if we just logged it,
+                    // we handled it in either way --> add to handled
+                    handled.add(event);
+                    unhandled.remove(event);
+                }
+            } while (!unhandled.isEmpty());
+
+            if (!throwableList.isEmpty())
             {
-                if (init)
+                PartialResponseWriter partialWriter = null;
+
+                //Retrieve the partial writer used to render the exception
+                if (facesContext == null)
+                {
+                    facesContext = FacesContext.getCurrentInstance();
+                }
+                
+                facesContext = (facesContext == null) ? FacesContext.getCurrentInstance() : facesContext;
+                ExternalContext externalContext = facesContext.getExternalContext();
+                
+                //Clean up the response if by some reason something has been written before.
+                if (!facesContext.getExternalContext().isResponseCommitted())
+                {
+                    facesContext.getExternalContext().responseReset();
+                }
+                
+                PartialViewContext partialViewContext = facesContext.getPartialViewContext();
+                partialWriter = partialViewContext.getPartialResponseWriter();
+
+                // ajax request --> xml error page 
+                externalContext.setResponseContentType("text/xml");
+                externalContext.setResponseCharacterEncoding("UTF-8");
+                externalContext.addResponseHeader("Cache-control", "no-cache");
+                
+                try
+                {
+                    partialWriter.startDocument();
+    
+                    for (Throwable t : throwableList)
+                    {
+                        renderAjaxError(partialWriter, t);
+                    }
+                }
+                catch (IOException e)
+                {
+                    if (log.isLoggable(Level.SEVERE))
+                    {
+                        log.log(Level.SEVERE, "Cannot render exception on ajax request", e);
+                    }
+                }
+                finally
                 {
                     try
                     {
                         partialWriter.endDocument();
                     }
-                    catch (IOException ioe)
+                    catch (IOException e1)
                     {
-                        throw new FacesException("Could not write the error page", ioe);
+                        if (log.isLoggable(Level.SEVERE))
+                        {
+                            log.log(Level.SEVERE, "Cannot render exception on ajax request", e1);
+                        }
                     }
                 }
             }
         }
     }
     
-    protected void renderAjaxError(ExceptionQueuedEvent event, PartialResponseWriter partialWriter, Throwable ex) throws IOException
+    private void renderAjaxError(PartialResponseWriter partialWriter, Throwable ex) throws IOException
     {
         partialWriter.startError(ex.getClass().getName());
         if (ex.getCause() != null)