error handling with spring + servlet spec

error-handling, error-reporting, spring, spring-mvc

Solution

- I use this approach in an application I'm currently working on, and it seems to work fine.

- 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.

Problem

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"> <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: - Whether this is the best approach to tackle errors. - 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.

Original source