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 2008/07/03 23:49:39 UTC

svn commit: r673832 - in /myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp: FacesServlet.java _ErrorPageWriter.java

Author: lu4242
Date: Thu Jul  3 14:49:39 2008
New Revision: 673832

URL: http://svn.apache.org/viewvc?rev=673832&view=rev
Log:
MYFACES-1888 catch Throwable errors when using ErrorPageWriter (myfaces error handling)

Modified:
    myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp/FacesServlet.java
    myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java

Modified: myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp/FacesServlet.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp/FacesServlet.java?rev=673832&r1=673831&r2=673832&view=diff
==============================================================================
--- myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp/FacesServlet.java (original)
+++ myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp/FacesServlet.java Thu Jul  3 14:49:39 2008
@@ -156,6 +156,10 @@
         {
             handleLifecycleException(facesContext, e);
         }
+        catch (Throwable e)
+        {
+            handleLifecycleThrowable(facesContext, e);
+        }
         finally
         {
             facesContext.release();
@@ -244,7 +248,9 @@
                 } catch (InstantiationException ex) {
                     throw new ServletException("Error-Handler : " +errorHandlerClass+ " could not be instantiated. Error-Handler is specified in web.xml-parameter : "+ERROR_HANDLER_PARAMETER,ex);
                 } catch (NoSuchMethodException ex) {
-                    throw new ServletException("Error-Handler : " +errorHandlerClass+ " did not have a method with name : handleException and parameters : javax.faces.context.FacesContext, java.lang.Exception. Error-Handler is specified in web.xml-parameter : "+ERROR_HANDLER_PARAMETER,ex);
+                    log.error("Error-Handler : " +errorHandlerClass+ " did not have a method with name : handleException and parameters : javax.faces.context.FacesContext, java.lang.Exception. Error-Handler is specified in web.xml-parameter : "+ERROR_HANDLER_PARAMETER,ex);
+                    //Try to look if it is implemented more general method handleThrowable
+                    handleLifecycleThrowable(facesContext, e);
                 } catch (InvocationTargetException ex) {
                     throw new ServletException("Excecution of method handleException in Error-Handler : " +errorHandlerClass+ " caused an exception. Error-Handler is specified in web.xml-parameter : "+ERROR_HANDLER_PARAMETER,ex);
                 }
@@ -257,6 +263,42 @@
             _ErrorPageWriter.throwException(e);
         }
     }
+    
+    private void handleLifecycleThrowable(FacesContext facesContext, Throwable e) throws IOException, ServletException {
+
+        boolean errorHandling = getBooleanValue(facesContext.getExternalContext().getInitParameter(ERROR_HANDLING_PARAMETER), true);
+
+        if(errorHandling) {
+            String errorHandlerClass = facesContext.getExternalContext().getInitParameter(ERROR_HANDLER_PARAMETER);            
+            if(errorHandlerClass != null) {
+                try {
+                    Class clazz = Class.forName(errorHandlerClass);
+
+                    Object errorHandler = clazz.newInstance();
+
+                    Method m = clazz.getMethod("handleThrowable", new Class[]{FacesContext.class,Throwable.class});
+                    m.invoke(errorHandler, new Object[]{facesContext, e});
+                }
+                catch(ClassNotFoundException ex) {
+                    throw new ServletException("Error-Handler : " +errorHandlerClass+ " was not found. Fix your web.xml-parameter : "+ERROR_HANDLER_PARAMETER,ex);
+                } catch (IllegalAccessException ex) {
+                    throw new ServletException("Constructor of error-Handler : " +errorHandlerClass+ " is not accessible. Error-Handler is specified in web.xml-parameter : "+ERROR_HANDLER_PARAMETER,ex);
+                } catch (InstantiationException ex) {
+                    throw new ServletException("Error-Handler : " +errorHandlerClass+ " could not be instantiated. Error-Handler is specified in web.xml-parameter : "+ERROR_HANDLER_PARAMETER,ex);
+                } catch (NoSuchMethodException ex) {
+                    throw new ServletException("Error-Handler : " +errorHandlerClass+ " did not have a method with name : handleException and parameters : javax.faces.context.FacesContext, java.lang.Exception. Error-Handler is specified in web.xml-parameter : "+ERROR_HANDLER_PARAMETER,ex);
+                } catch (InvocationTargetException ex) {
+                    throw new ServletException("Excecution of method handleException in Error-Handler : " +errorHandlerClass+ " threw an exception. Error-Handler is specified in web.xml-parameter : "+ERROR_HANDLER_PARAMETER,ex);
+                }
+            }
+            else {
+                _ErrorPageWriter.handleThrowable(facesContext, e);
+            }
+        }
+        else {
+            _ErrorPageWriter.throwException(e);
+        }
+    }
 
     private static boolean getBooleanValue(String initParameter, boolean defaultVal) {
 

Modified: myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java?rev=673832&r1=673831&r2=673832&view=diff
==============================================================================
--- myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java (original)
+++ myfaces/core/trunk_1.2.x/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java Thu Jul  3 14:49:39 2008
@@ -84,7 +84,7 @@
         return str.split("@@");
     }
 
-    private static ArrayList getErrorId(Exception e){
+    private static ArrayList getErrorId(Throwable e){
         String message = e.getMessage();
 
         if(message==null)
@@ -115,8 +115,8 @@
             writer.write(ex.getClass().getName());
         }
     }
-    
-    public static void debugHtml(Writer writer, FacesContext faces, Exception e) throws IOException {
+
+    public static void debugHtml(Writer writer, FacesContext faces, Throwable e) throws IOException {
         init();
         Date now = new Date();
         for (int i = 0; i < ERROR_PARTS.length; i++) {
@@ -226,7 +226,7 @@
         }
     }    
 
-    private static void writeException(Writer writer, Exception e) throws IOException {
+    private static void writeException(Writer writer, Throwable e) throws IOException {
         StringWriter str = new StringWriter(256);
         PrintWriter pstr = new PrintWriter(str);
         e.printStackTrace(pstr);
@@ -414,7 +414,12 @@
         return (c.getClass().getName().startsWith("com.sun.facelets.compiler"));
     }
 
-    public static void handleException(FacesContext facesContext, Exception ex) throws ServletException, IOException {
+    public static void handleException(FacesContext facesContext, Exception ex) throws ServletException, IOException
+    {
+        handleThrowable(facesContext, ex);
+    }
+    
+    public static void handleThrowable(FacesContext facesContext, Throwable ex) throws ServletException, IOException {
 
         prepareExceptionStack(ex);
 
@@ -497,7 +502,7 @@
         }
     }
 
-    static void throwException(Exception e) throws IOException, ServletException {
+    static void throwException(Throwable e) throws IOException, ServletException {
 
         prepareExceptionStack(e);