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

  • Sending mail on uncaught exception


    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:

    <bean id="handlerExceptionResolver" class="com.yourpackage.web.exception.ExceptionResolver">
     <property name="defaultErrorView">
     <property name="exceptionAttribute">
     <property name="mailers"><ref bean="mailers" /></property>
     <property name="exceptionMappings">
          <prop key="com.yourpackage.sitemap.SitemapPageNotFoundException">exception/page.not.found</prop>
          <prop key="">exception/user.not.authorized</prop>
          <prop key="org.springframework.dao.DataIntegrityViolationException">exception/data.integrity.error</prop>
    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:
    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
    <bean id="dwrUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="alwaysUseFullPath" value="true"/>
      <property name="mappings">
          <prop key="/dwr/**/*">dwrController</prop>
    Spring would catch the errors just like any other Controller. Note that you'll have to define a dwrController bean.


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