Announcement Announcement Module
Collapse
No announcement yet.
Sending mail on uncaught exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sending mail on uncaught exception

    Hello,

    i want to send an e-mail to admin of server, when unhandled exception is thrown.

    I want to use logger with mail appender in spring controller rather than using logger in error.jsp

    I tried to extend exception resolver, but this cover only expcetions thrown in spring servlet, but not for others. (DWR for example)

    Because i need spring locale resolver, i want to spring controller handle error #500 of server. But i do not known how can i access thrown exceptions in that controller.

    Is there any solution for this?

    Thank you very much for your advices.

    Best regards,
    Andrej Zachar

  • #2
    Catching DWR exceptions can be tricky depending how you setup DWR. For Spring to send emails via a ExceptionResolver, it is pretty simple, just have the ExeptionResolver defined, something like:

    Code:
    <bean id="handlerExceptionResolver" class="com.yourpackage.web.exception.ExceptionResolver">
     <property name="defaultErrorView">
        <value>exception/exception</value>
     </property>
     <property name="exceptionAttribute">
       <value>exception</value></property>
     <property name="mailers"><ref bean="mailers" /></property>
     <property name="exceptionMappings">
        <props>
          <prop key="com.yourpackage.sitemap.SitemapPageNotFoundException">exception/page.not.found</prop>
          <prop key="com.yourpackage.security.interceptor.PrincipalNotAuthorizedException">exception/user.not.authorized</prop>
          <prop key="org.springframework.dao.DataIntegrityViolationException">exception/data.integrity.error</prop>
        </props>
      </property>
    </bean>
    In this case, Mailers is just a Service Interface which defines a service like sendExceptionEmail( Exception e ). It just calls into whatever strategy you use to send emails.

    If you do not have DWR configured as a bean inside Spring, but have it configured instead via the web.xml (which is common), you won't be able to use the ExceptionResolver to catch the errors. For example, if DWR is setup like this in web.xml:
    Code:
    	<servlet>
    	    <servlet-name>dwr</servlet-name>
    	    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    	    <init-param>
    			<param-name>debug</param-name>
    			<param-value>true</param-value>
    	    </init-param>
    	</servlet>
    Spring won't be able to catch exceptions because they'll go right to the servlet container. If you have DWR setup as a bean in Spring, something like
    Code:
    <bean id="dwrUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="alwaysUseFullPath" value="true"/>
      <property name="mappings">
        <props>
          <prop key="/dwr/**/*">dwrController</prop>
        </props>
      </property>
    </bean>
    Spring would catch the errors just like any other Controller. Note that you'll have to define a dwrController bean.

    Comment


    • #3
      You can do this with a log4j setup using a SmtpAppender. Your log messages can then be filtered by class, and sent to any of various destination email adresses. You should implement commons-loggging and back it with a log4j implementation.

      Comment

      Working...
      X