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 21:35:15 UTC
svn commit: r673775 - in /myfaces/core/trunk/api/src/main/java/javax/faces:
component/UIInput.java webapp/FacesServlet.java webapp/_ErrorPageWriter.java
Author: lu4242
Date: Thu Jul 3 12:35:14 2008
New Revision: 673775
URL: http://svn.apache.org/viewvc?rev=673775&view=rev
Log:
MYFACES-1887 Print Multiple exceptions information thrown on update model
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
myfaces/core/trunk/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.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=673775&r1=673774&r2=673775&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 Thu Jul 3 12:35:14 2008
@@ -343,11 +343,10 @@
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));
}
+ li.add(new FacesException("Exception while setting value for expression : "+
+ binding.getExpressionString()+" of component with path : "
+ + _ComponentUtils.getPathToComponent(this),e));
}
protected void validateValue(FacesContext context,Object convertedValue)
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=673775&r1=673774&r2=673775&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 Thu Jul 3 12:35:14 2008
@@ -142,9 +142,10 @@
try {
_lifecycle.execute(facesContext);
- handleQueuedExceptions(facesContext);
-
- _lifecycle.render(facesContext);
+ if (!handleQueuedExceptions(facesContext))
+ {
+ _lifecycle.render(facesContext);
+ }
}
catch (Exception e)
{
@@ -169,7 +170,7 @@
* @param facesContext
* @throws javax.faces.FacesException
*/
- private void handleQueuedExceptions(FacesContext facesContext) throws FacesException {
+ private boolean handleQueuedExceptions(FacesContext facesContext) throws IOException, ServletException {
List li = (List)
facesContext.getExternalContext().getRequestMap().get(ERROR_HANDLING_EXCEPTION_LIST);
@@ -177,12 +178,45 @@
//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);
+ 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("handleExceptionList", new Class[]{FacesContext.class,Exception.class});
+ m.invoke(errorHandler, new Object[]{facesContext, li});
+ }
+ 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) {
+ //Handle in the old way, since no custom method handleExceptionList found,
+ //throwing the first FacesException on the list.
+ throw (FacesException) li.get(0);
+ } 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.handleExceptionList(facesContext, li);
+ }
+ }
+ else {
+ _ErrorPageWriter.throwException((Exception) li.get(0));
+ }
+ return true;
}
+ return false;
}
-
-
private void handleLifecycleException(FacesContext facesContext, Exception e) throws IOException, ServletException {
boolean errorHandling = getBooleanValue(facesContext.getExternalContext().getInitParameter(ERROR_HANDLING_PARAMETER), true);
Modified: myfaces/core/trunk/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java?rev=673775&r1=673774&r2=673775&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/webapp/_ErrorPageWriter.java Thu Jul 3 12:35:14 2008
@@ -164,6 +164,87 @@
}
}
}
+
+ public static void debugHtml(Writer writer, FacesContext faces, List exceptionList) throws IOException
+ {
+ init();
+ Date now = new Date();
+ for (int i = 0; i < ERROR_PARTS.length; i++)
+ {
+ if ("message".equals(ERROR_PARTS[i]))
+ {
+ for (int j = 0; j < exceptionList.size(); j++)
+ {
+ Exception e = (Exception) exceptionList.get(j);
+ String msg = e.getMessage();
+ if (msg != null)
+ {
+ writer.write(msg.replaceAll("<", TS));
+ }
+ else
+ {
+ writer.write(e.getClass().getName());
+ }
+ if (!(j+1==exceptionList.size()))
+ {
+ writer.write("<br>");
+ }
+ }
+ }
+ else if ("trace".equals(ERROR_PARTS[i]))
+ {
+ for (int j = 0; j < exceptionList.size(); j++)
+ {
+ Exception e = (Exception) exceptionList.get(j);
+ writeException(writer, e);
+ }
+ }
+ else if ("now".equals(ERROR_PARTS[i]))
+ {
+ writer.write(DateFormat.getDateTimeInstance().format(now));
+ }
+ else if ("tree".equals(ERROR_PARTS[i]))
+ {
+ if (faces.getViewRoot() != null)
+ {
+ List highlightId = null;
+ for (int j = 0; j < exceptionList.size(); j++)
+ {
+ Exception e = (Exception) exceptionList.get(j);
+ if (highlightId == null)
+ {
+ highlightId = getErrorId(e);
+ }
+ else
+ {
+ highlightId.addAll(getErrorId(e));
+ }
+ }
+ writeComponent(writer, faces.getViewRoot(), highlightId);
+ }
+ }
+ else if ("vars".equals(ERROR_PARTS[i]))
+ {
+ writeVariables(writer, faces);
+ }
+ else if ("cause".equals(ERROR_PARTS[i]))
+ {
+ for (int j = 0; j < exceptionList.size(); j++)
+ {
+ Exception e = (Exception) exceptionList.get(j);
+ writeCause(writer, e);
+ if (!(j+1==exceptionList.size()))
+ {
+ writer.write("<br>");
+ }
+ }
+ }
+ else
+ {
+ writer.write(ERROR_PARTS[i]);
+ }
+ }
+ }
private static void writeException(Writer writer, Exception e) throws IOException {
StringWriter str = new StringWriter(256);
@@ -382,6 +463,41 @@
throwException(ex);
}
}
+
+ public static void handleExceptionList(FacesContext facesContext, List exceptionList) throws ServletException, IOException
+ {
+ for (int i = 0; i < exceptionList.size(); i++)
+ {
+ prepareExceptionStack( (Exception) exceptionList.get(i));
+ }
+
+ Object response = facesContext.getExternalContext().getResponse();
+ if(response instanceof HttpServletResponse)
+ {
+ HttpServletResponse httpResp = (HttpServletResponse) response;
+ if (!httpResp.isCommitted())
+ {
+ httpResp.reset();
+ httpResp.setContentType("text/html; charset=UTF-8");
+ Writer writer = httpResp.getWriter();
+
+ debugHtml(writer, facesContext, exceptionList);
+
+ for (int i = 0; i < exceptionList.size(); i++)
+ {
+ log.error("An exception occurred", (Exception) exceptionList.get(i));
+ }
+ }
+ else
+ {
+ throwException((Exception)exceptionList.get(0));
+ }
+ }
+ else
+ {
+ throwException((Exception)exceptionList.get(0));
+ }
+ }
private static void prepareExceptionStack(Throwable ex) {