Author: mrdon Date: Sat Jun 10 16:50:15 2006 New Revision: 413388 URL: http://svn.apache.org/viewvc?rev=413388&view=rev Log: Adding support for arbitrary exceptions, including showing snippets of the problematic Java code if source is available
Modified: struts/action2/trunk/core/src/main/java/org/apache/struts/action2/dispatcher/DispatcherUtils.java struts/action2/trunk/core/src/main/resources/org/apache/struts/action2/dispatcher/error.ftl Modified: struts/action2/trunk/core/src/main/java/org/apache/struts/action2/dispatcher/DispatcherUtils.java URL: http://svn.apache.org/viewvc/struts/action2/trunk/core/src/main/java/org/apache/struts/action2/dispatcher/DispatcherUtils.java?rev=413388&r1=413387&r2=413388&view=diff ============================================================================== --- struts/action2/trunk/core/src/main/java/org/apache/struts/action2/dispatcher/DispatcherUtils.java (original) +++ struts/action2/trunk/core/src/main/java/org/apache/struts/action2/dispatcher/DispatcherUtils.java Sat Jun 10 16:50:15 2006 @@ -35,6 +35,8 @@ import com.opensymphony.xwork.interceptor.component.ComponentManager; import com.opensymphony.xwork.util.*; import com.opensymphony.xwork.util.location.Location; +import com.opensymphony.xwork.util.location.LocationUtils; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -95,23 +97,23 @@ } protected void cleanup() { - ObjectFactory objectFactory = ObjectFactory.getObjectFactory(); - if (objectFactory == null) { - LOG.warn("Object Factory is null, something is seriously wrong, no clean up will be performed"); - } - if (objectFactory instanceof ObjectFactoryDestroyable) { - try { - ((ObjectFactoryDestroyable)objectFactory).destroy(); - } - catch(Exception e) { - // catch any exception that may occured during destroy() and log it - LOG.error("exception occurred while destroying ObjectFactory ["+objectFactory+"]", e); - } - } + ObjectFactory objectFactory = ObjectFactory.getObjectFactory(); + if (objectFactory == null) { + LOG.warn("Object Factory is null, something is seriously wrong, no clean up will be performed"); + } + if (objectFactory instanceof ObjectFactoryDestroyable) { + try { + ((ObjectFactoryDestroyable)objectFactory).destroy(); + } + catch(Exception e) { + // catch any exception that may occured during destroy() and log it + LOG.error("exception occurred while destroying ObjectFactory ["+objectFactory+"]", e); + } + } } protected void init(ServletContext servletContext) { - boolean reloadi18n = Boolean.valueOf((String) Configuration.get(StrutsConstants.STRUTS_I18N_RELOAD)).booleanValue(); + boolean reloadi18n = Boolean.valueOf((String) Configuration.get(StrutsConstants.STRUTS_I18N_RELOAD)).booleanValue(); LocalizedTextUtil.setReloadBundles(reloadi18n); if (Configuration.isSet(StrutsConstants.STRUTS_OBJECTFACTORY)) { @@ -256,9 +258,8 @@ LOG.error("Could not find action", e); sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e); } catch (Exception e) { - String msg = "Could not execute action"; - LOG.error(msg, e); - throw new ServletException(msg, e); + LOG.error("Could not execute action", e); + sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); } } @@ -480,10 +481,15 @@ data.put("exception", e); data.put("unknown", Location.UNKNOWN); data.put("chain", chain); + data.put("locator", new Locator()); template.process(data, response.getWriter()); response.getWriter().close(); } catch (Exception exp) { - exp.printStackTrace(); + try { + response.sendError(code, "Unable to show problem report: " + exp); + } catch (IOException ex) { + // we're already sending an error, not much else we can do if more stuff breaks + } } } else { try { @@ -510,5 +516,11 @@ public static boolean isPortletSupportActive() { return portletSupportActive; } - + + /** Simple accessor for a static method */ + public class Locator { + public Location getLocation(Throwable t) { + return LocationUtils.getLocation(t); + } + } } Modified: struts/action2/trunk/core/src/main/resources/org/apache/struts/action2/dispatcher/error.ftl URL: http://svn.apache.org/viewvc/struts/action2/trunk/core/src/main/resources/org/apache/struts/action2/dispatcher/error.ftl?rev=413388&r1=413387&r2=413388&view=diff ============================================================================== --- struts/action2/trunk/core/src/main/resources/org/apache/struts/action2/dispatcher/error.ftl (original) +++ struts/action2/trunk/core/src/main/resources/org/apache/struts/action2/dispatcher/error.ftl Sat Jun 10 16:50:15 2006 @@ -5,7 +5,7 @@ <body> <h2>Struts Action Framework Problem Report</h2> <p> - The Struts Action Framework has detected a problem in your configuration: + The Struts Action Framework has detected an unhandled exception: </p> <#assign msgs = [] /> @@ -19,6 +19,12 @@ <#if (ex.location?exists && (ex.location != unknown))> <#assign rootloc = ex.location/> <#assign rootex = ex/> + <#else> + <#assign tmploc = locator.getLocation(ex) /> + <#if (tmploc != unknown)> + <#assign rootloc = tmploc/> + <#assign rootex = ex/> + </#if> </#if> </#list> @@ -33,8 +39,8 @@ <li>${msg}</li> </#list> </ol> - <#else> - ${msgs[0]} + <#elseif (msgs?size == 1)> + ${msgs[0]} </#if> </td> </tr> @@ -47,24 +53,30 @@ <td><strong>Line number</strong>:</td> <td>${rootloc.lineNumber}</td> </tr> + <#if (rootloc.columnNumber >= 0)> <tr> <td><strong>Column number</strong>:</td> <td>${rootloc.columnNumber}</td> </tr> </#if> + </#if> </table> </div> -<#if rootex.snippet?exists> - <#assign snippet = rootex.getSnippet(2) /> +<#if rootloc?exists> + <#assign snippet = rootloc.getSnippet(2) /> <#if (snippet?size > 0)> <div id="snippet"> <hr /> <#list snippet as line> <#if (line_index == 2)> - <pre style="background:yellow">${(line[0..(rootloc.columnNumber-3)]?html)}<span style="background:red">${(line[(rootloc.columnNumber-2)]?html)}</span><#if ((rootloc.columnNumber)<line.length())>${(line[(rootloc.columnNumber-1)..]?html)}</#if></pre> + <#if (rootloc.columnNumber >= 0)> + <pre style="background:yellow">${(line[0..(rootloc.columnNumber-3)]?html)}<span style="background:red">${(line[(rootloc.columnNumber-2)]?html)}</span><#if ((rootloc.columnNumber)<line.length())>${(line[(rootloc.columnNumber-1)..]?html)}</#if></pre> + <#else> + <pre style="background:yellow">${line?html}</pre> + </#if> <#else> <pre>${line?html}</pre> </#if>