Class ExceptionToMailResolver

java.lang.Object
org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
com.foreach.across.modules.logging.exception.ExceptionToMailResolver
All Implemented Interfaces:
org.springframework.core.Ordered, org.springframework.web.servlet.HandlerExceptionResolver

public class ExceptionToMailResolver extends org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
ExceptionToMailResolver sends a mail for every java exception that is not caught at the controller level.

To use this resolver, declare a bean for this class in your spring configuration file.

Example spring configuration:

  <bean class="com.foreach.web.logging.ExceptionToMailResolver">
      <property name="fromAddress" value="${errormail.from}"/>
      <property name="toAddress" value="${erromail.to}"/>
      <property name="order" value="1"/>
      <property name="exceptionMappings">
          <props>
              <prop key="java.lang.Throwable">error</prop>
          </props>
      </property>
      <property name="mailService" ref="mailService"/>
      <property name="applicationContextInfo" ref="applicationContext"/>
  </bean>
 

When you create an instance, following properties are mandatory:

  • fromAddress: you may want to have a different sender for each application and environment combination to facilitate filtering, so
    smurfvillage-staging-errors@foo.bar might be a better idea than noreply@foo.bar.
  • toAddress: usually the operators or developers.
  • order: see AbstractHandlerExceptionResolver
  • exceptionMappings: all the exceptions caught by this exception resolver. see AbstractHandlerExceptionResolver
  • mailService: the name of a MailService bean
  • applicationContextInfo: the name of the ApplicationContextInfo bean
  • Field Summary

    Fields inherited from class org.springframework.web.servlet.handler.SimpleMappingExceptionResolver

    DEFAULT_EXCEPTION_ATTRIBUTE

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final org.springframework.web.servlet.ModelAndView
    doResolveException(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler, Exception ex)
    protected final org.slf4j.Logger
    Get the logger
    final void
    setApplicationContext(com.foreach.common.spring.context.ApplicationContextInfo context)
    set the ApplicationContextInfo object holding the properties of current running application
    final void
    setFromAddress(String fromAddress)
    Specify from email address
    protected final void
    setLogger(org.slf4j.Logger logger)
    Specify your own custom logger
    final void
    setMailService(com.foreach.common.spring.mail.MailService mailService)
    set the mail service, which actually sends the exception mail
    final void
    setToAddress(String toAddress)
    Specify to email address

    Methods inherited from class org.springframework.web.servlet.handler.SimpleMappingExceptionResolver

    addStatusCode, applyStatusCodeIfPossible, determineStatusCode, determineViewName, findMatchingViewName, getDepth, getModelAndView, getModelAndView, getStatusCodesAsMap, setDefaultErrorView, setDefaultStatusCode, setExceptionAttribute, setExceptionMappings, setExcludedExceptions, setStatusCodes

    Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver

    addMappedHandlerClass, buildLogMessage, getMappedHandlerClasses, getOrder, hasHandlerMappings, logException, prepareResponse, preventCaching, resolveException, setMappedHandlerClasses, setMappedHandlerPredicate, setMappedHandlers, setOrder, setPreventResponseCaching, setWarnLogCategory, shouldApplyTo

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ExceptionToMailResolver

      public ExceptionToMailResolver()
  • Method Details

    • setLogger

      protected final void setLogger(org.slf4j.Logger logger)
      Specify your own custom logger
      Parameters:
      logger -
    • getLogger

      protected final org.slf4j.Logger getLogger()
      Get the logger
      Returns:
      Logger
    • setFromAddress

      public final void setFromAddress(String fromAddress)
      Specify from email address
      Parameters:
      fromAddress -
    • setToAddress

      public final void setToAddress(String toAddress)
      Specify to email address
      Parameters:
      toAddress -
    • setMailService

      public final void setMailService(com.foreach.common.spring.mail.MailService mailService)
      set the mail service, which actually sends the exception mail
      Parameters:
      mailService -
    • setApplicationContext

      public final void setApplicationContext(com.foreach.common.spring.context.ApplicationContextInfo context)
      set the ApplicationContextInfo object holding the properties of current running application
      Parameters:
      context -
    • doResolveException

      public final org.springframework.web.servlet.ModelAndView doResolveException(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler, Exception ex)
      Overrides:
      doResolveException in class org.springframework.web.servlet.handler.SimpleMappingExceptionResolver