Results 1 to 4 of 4

Thread: Logging exceptions

  1. #1
    Join Date
    Feb 2006
    Location
    Nancy, France
    Posts
    145

    Default Logging exceptions

    Hello,

    In my web application, I have a SimpleMappingExceptionResolver which handles any RuntimeException (like DataAccessExceptions).
    It works fine but how could I tell it I want all MyExceptions to be logged ?

    The only way I found is to subclass SimpleMappingExceptionResolver and override resolveException method.

  2. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    From the javadoc (http://www.springframework.org/docs/...il.Properties)) it would appear that you simply need to specify "MyException" which will catch instances of "anything.you.want.MyException" and *all* subclasses.

    So to catch all exceptions you would specify "Exception".

    I imagine if you wanted to have all implementations (i.e. subclasses) of MyException to go to "myExceptionView" and anything else to go to "genericExceptionView" then you would specify the first property as "MyException=myExceptionView" and the second property as "Exception=myExceptionView" although I haven't tried it but I imagine it would work.

    If that doesn't work then because SimpleMappingExceptionREsolver implements the Ordered interface you can define two exceptionResolvers; the first (i.e. order=0) with the more specific (i.e. MyException) exception and a second (i.e. order=1) for the catch all (i.e. Exception).

    HTH.
    Last edited by Colin Yates; Jul 25th, 2006 at 02:57 AM.
    Colin Yates
    SpringSource - http://www.springsource.com - Spring Training, Consulting, and Support - "From the Source"
    Please read http://www.springframework.org/documentation
    Co-Author of Expert Spring MVC + Web Flow.

  3. #3
    Join Date
    Feb 2006
    Location
    Nancy, France
    Posts
    145

    Default

    I think there's a little misunderstanding.
    I have no problem to associate different views to specifics exceptions.
    My problem is that I can't log these exceptions, except in the view itself but I don't want to put specific Java code into JSP.

  4. #4

    Default

    I solved this problem by doing exactly what you said, subclassing the SimpleMappingExceptionResolver.

    Here's my solution

    Code:
    public class LoggingMappingExceptionResolver extends SimpleMappingExceptionResolver{
    
        private static final Log log = LogFactory.getLog(LoggingMappingExceptionResolver.class);
    	
    	
    	public ModelAndView resolveException(
    		    HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) 
    	{
    		log.error("Unhandled Exception", ex);
    		return super.resolveException(request, response, handler, ex);
    	}
    }
    If there's a more elegant solution, I'd surely like to know!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •