You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2007/09/28 17:15:21 UTC

svn commit: r580384 - in /myfaces/core/trunk/api/src/main/java/javax/faces: component/UIInput.java webapp/FacesServlet.java

Author: mmarinschek
Date: Fri Sep 28 08:15:11 2007
New Revision: 580384

URL: http://svn.apache.org/viewvc?rev=580384&view=rev
Log:
MYFACES-1736 (https://issues.apache.org/jira/browse/MYFACES-1736): UIInput.updateModel may not throw exceptions according to Spec - allow error-handling nevertheless

Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/UIInput.java
    myfaces/core/trunk/api/src/main/java/javax/faces/webapp/FacesServlet.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/UIInput.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/UIInput.java?rev=580384&r1=580383&r2=580384&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/UIInput.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/UIInput.java Fri Sep 28 08:15:11 2007
@@ -47,6 +47,7 @@
 {
     public static final String CONVERSION_MESSAGE_ID = "javax.faces.component.UIInput.CONVERSION";
     public static final String REQUIRED_MESSAGE_ID = "javax.faces.component.UIInput.REQUIRED";
+    private static final String ERROR_HANDLING_EXCEPTION_LIST = "org.apache.myfaces.errorHandling.exceptionList";
 
     private static final Validator[] EMPTY_VALIDATOR_ARRAY = new Validator[0];
 
@@ -278,9 +279,38 @@
             setValue(null);
             setLocalValueSet(false);
         }
-        catch (Exception ex)
+         catch (Exception e)
         {
-            throw new FacesException("Exception while setting value : "+vb.getExpressionString()+" of component with path : "+_ComponentUtils.getPathToComponent(this),ex);
+        	//Object[] args = {getId()};
+            context.getExternalContext().log(e.getMessage(), e);
+            _MessageUtils.addErrorMessage(context, this,CONVERSION_MESSAGE_ID,new Object[]{getId()});
+            setValid(false);
+
+            /* we are not allowed to throw exceptions here - we still need the full stack-trace later on
+             * to process it later in our error-handler
+             */
+            queueExceptionInRequest(context, vb, e);
+        }
+    }
+
+    /**
+     * For development and production, we want to offer a single point
+     * to which error-handlers can attach. So we queue up all ocurring
+     * exceptions and later pass them to the configured error-handler.
+     *
+     * @param context
+     * @param binding
+     * @param e
+     */
+    private void queueExceptionInRequest(FacesContext context, ValueBinding binding, Exception e) {
+        List li = (List) context.getExternalContext().getRequestMap().get(ERROR_HANDLING_EXCEPTION_LIST);
+        if(null==li) {
+            li = new ArrayList();
+            context.getExternalContext().getRequestMap().put(ERROR_HANDLING_EXCEPTION_LIST, li);
+
+            li.add(new FacesException("Exception while setting value for expression : "+
+                binding.getExpressionString()+" of component with path : "
+                + _ComponentUtils.getPathToComponent(this),e));
         }
     }
 

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/webapp/FacesServlet.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/webapp/FacesServlet.java?rev=580384&r1=580383&r2=580384&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/webapp/FacesServlet.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/webapp/FacesServlet.java Fri Sep 28 08:15:11 2007
@@ -21,8 +21,10 @@
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.lang.reflect.InvocationTargetException;
+import java.util.List;
 
 import javax.faces.FactoryFinder;
+import javax.faces.FacesException;
 import javax.faces.context.FacesContext;
 import javax.faces.context.FacesContextFactory;
 import javax.faces.lifecycle.Lifecycle;
@@ -54,6 +56,7 @@
     private static final String SERVLET_INFO = "FacesServlet of the MyFaces API implementation";
     private static final String ERROR_HANDLING_PARAMETER = "org.apache.myfaces.ERROR_HANDLING";
     private static final String ERROR_HANDLER_PARAMETER = "org.apache.myfaces.ERROR_HANDLER";
+    private static final String ERROR_HANDLING_EXCEPTION_LIST = "org.apache.myfaces.errorHandling.exceptionList";    
 
     private ServletConfig _servletConfig;
     private FacesContextFactory _facesContextFactory;
@@ -138,7 +141,10 @@
         FacesContext facesContext = prepareFacesContext(request, response);
         try {
 			_lifecycle.execute(facesContext);
-			_lifecycle.render(facesContext);
+
+            handleQueuedExceptions(facesContext);
+
+            _lifecycle.render(facesContext);
 		}
         catch (Exception e)
         {
@@ -150,6 +156,32 @@
         }
 		if(log.isTraceEnabled()) log.trace("service end");
     }
+
+    /**This method makes sure we see an exception page also
+     * if an exception has been thrown in UIInput.updateModel();
+     * In this method, according to the spec, we may not rethrow the
+     * exception, so we add it to a list and process it here.
+     *
+     * Attention: if you use redirects, the exceptions will get lost - exactly
+     * like in the case of FacesMessages. If you want them to be taken over to the
+     * next request, you should try the redirect-tracker of MyFaces.
+     *
+     * @param facesContext
+     * @throws javax.faces.FacesException
+     */
+    private void handleQueuedExceptions(FacesContext facesContext) throws FacesException {
+        List li = (List)
+                facesContext.getExternalContext().getRequestMap().get(ERROR_HANDLING_EXCEPTION_LIST);
+
+        if(li != null && li.size()>=1)  {
+            //todo: for now, we only handle the first exception out of the list - we just rethrow this
+            //first exception.
+            //in the end, we should enable the error handler to show all the exceptions at once
+            throw (FacesException) li.get(0);
+        }
+    }
+
+    
 
     private void handleLifecycleException(FacesContext facesContext, Exception e) throws IOException, ServletException {