Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Build a better Roo exception page

DZone's Guide to

Build a better Roo exception page

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Ok, Roo fans. This is a quick post, in response to a forum post on the Manning Roo in Action forum. Wanna get Root Causes in your exceptions page? Do this!

Build an Exception Resolver

Start by building a class that extends SimpleMappingExceptionResolver. This example uses the Apache commons-lang utility class, ExceptionUtils, to get the root cause exception and push it to the page. I suspect I may need to check for nulls here so if you get a chance to debug this before me, post a message and I'll update the blog post:

public class FullTraceMappingExceptionResolver extends
SimpleMappingExceptionResolver {

@Override
protected ModelAndView getModelAndView(String viewName, Exception ex) {
ModelAndView modelAndView = super.getModelAndView(viewName, ex);
String stackTrace = ExceptionUtils.getFullStackTrace(ExceptionUtils.getRootCause(ex));
modelAndView.addObject("rootCause", stackTrace);
return modelAndView;
}
}

 

Add commons-lang 2.5

Add this to your pom.xml file, in the dependencies section... A Roo shell command can do this for you, but here is the fragment:

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>

Install it!

Next, install it in the webmvc-config.xml file, by replacing the mounted class SimpleMappingExceptionResolver with the FullTraceMappingExceptionResolver.  Just swap the value in the class property below and leave the rest alone:

<bean class="package.path.to.FullTraceMappingExceptionResolver"
p:defaultErrorView="uncaughtException">
<property name="exceptionMappings">
<props>
<prop key=".DataAccessException">dataAccessFailure</prop>
<prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop>
<prop key=".TypeMismatchException">resourceNotFound</prop>
<prop key=".MissingServletRequestParameterException">resourceNotFound</prop>
</props>
</property>
</bean>

Modify unhandledException.jspx

Ok, the final step - just add the unhandled exception to the jspx for the uncaught exceptions, WEB-INF/views/uncaughtException.jspx

<c:if test="${not empty rootCause}">
<util:panel id="_fullStackTrace"
title="Full Stack Trace" openPane="false">
<h4>Full Stack Trace</h4>
<br/>
<div style="overflow: auto;"
dojoType="dijit.layout.ContentPane">
<pre><c:out value="${rootCause}"/></pre>
</div>
</util:panel>
</c:if>

 

Want more Roo in Action goodness? Subscribe to the MEAP!

 

From http://www.rimple.com/tech/2011/3/15/build-a-better-roo-exception-page.html

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}