views:

758

answers:

1

I have a web-app (2.5 servlet spec) with a spring dispatcherservlet handling anything coming on /error/* and an error page configured to route that to /error/ something like this:

<servlet>
    <servlet-name>errorServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>errorServlet</servlet-name>
    <url-pattern>/erorr/*</url-pattern>
</servlet-mapping>
<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/erorr/</location>
</error-page>

and the errorServlet-servelt.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
    <context:annotation-config/>
    <context:component-scan base-package="some.base.package"/>
    <bean id="simpleUrlController" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/*">errorController</prop>
            </props>
        </property>
    </bean>
    <bean id="errorController" class="ErrorController">
        <property name="formView" value="formView"/>
        <property name="commandClass" value="Error"/>
        <property name="commandName" value="errorNAMe"/>
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

Spots I need help on:

  1. Whether this is the best approach to tackle errors.
  2. I know there is a SimpleMappingExceptionResolver which i can declare in my configuration...but i read somewhere that this class is good with only the exceptions coming from the spring controllers and not others.
+1  A: 
  1. I use this approach in an application I'm currently working on, and it seems to work fine.
  2. That is true, but it doesn't matter. If it's a web application, any exception that gets thrown will eventually bubble up to the top, which should be the Spring controller. It will then get handled from there based on your configuration, whether it's forwarding to another page or letting your application explode.

This is a pretty good tutorial on the basics; there are others if you google for it: http://developingdeveloper.wordpress.com/2008/03/09/handling-exceptions-in-spring-mvc-part-2/

EDIT: Instead of only redirecting to an error page, you could also put these exceptions in a database, so you have a list of the most common ones that occur. Joel and Jeff mention that they do this for StackOverflow, and that list becomes part of their bugs-to-fix list.

Alex Beardsley
Thanks Nalandial...About putting the exceptions in db...well...I am creating a log file on disk...and have a batch job sweep it and create a report for the support staff. the reason is I don't want to rely on any external system (as they might also fail ... but again may be i am getting paranoid...:-))
No you're right, I just meant it as a simple example of what you could do. There's actually a framework for .NET that does this fancy stuff for you, and I think there's one for Java too but I don't remember the name. It may be in the show notes/transcript wiki for one of Joel and Jeff's previous podcasts.
Alex Beardsley