Results 1 to 7 of 7

Thread: AOP Exception Handling

  1. #1
    Join Date
    Jan 2008
    Posts
    4

    Default AOP Exception Handling

    I am new to AOP and have been trying to create an AOP-based exception handling framework for my application. I know this question has been asked in these forums before, but I have not been able to determine why exceptions thrown in the code are not being logged. Essentially, I am just trying to log all exceptions that are thrown in the com.foo.bar package.

    Configuration:
    Code:
    <bean id="errorHandler" class="com.foo.bar.arch.error.ErrorHandler"/>
    <aop:aspectj-autoproxy/>
    <aop:config>
    	<aop:after-throwing
    			pointcut="within(com.foo.bar..*)"
    			throwing="exception" method="processError"/>
    	</aop:aspect>
    </aop:config>
    ErrorHandler:
    Code:
    public final class ErrorHandler
    {
        ...
        public void processError(JoinPoint logged, MyException exception)
        {
            logger.error(exception.getCause().getMessage());
        } 
        ... 
    }
    In order to test this, I am simply throwing a MyException in my test code:
    Code:
    public void testAOPErrorHandling()
    {
            try
            {
                throw new MyException("something");
            }
            catch(MyException e)
            {
                // do something
            }
    }
    Am I incorrect in catching the exception here? My understanding was that it would still be logged.

    Thanks!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    1. Do you programm to interfaces, if not enable proxy-target-class
    2. Why do you also use aspectj autoproxing? You use aop blocks to configure?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2008
    Posts
    4

    Default

    1. The class that is throwing the error is an implementation of an interface. Enabling proxy-target-class did not help (although it did alert me that several methods could not be logged because they are final - not the test class though).

    2. The AspectJ auto-proxying was a mistake, I picked it up somewhere in my attempts to figure out what was is wrong.

    Any other ideas?

    Thanks

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Code:
    <aop:config>
    	<aop:after-throwing
    			pointcut="within(com.foo.bar..*)"
    			throwing="exception" method="processError"/>
    	</aop:aspect>
    </aop:config>
    Ehrm is that your actual configuration?! Because if that is so I would expect your application to blow up because your xml is invalid.

    Also is the bean you are using (the one that throws the exception) configured in your application context if not spring isn't going to proxy anything, it can only intercept calls to object it itself manages, not objects created with new...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jan 2008
    Posts
    4

    Default

    Bad copy and paste on my part, the configuration is actually:

    Code:
    <aop:config>
        <aop:aspect ref="errorHandler">
    	<aop:after-throwing
    		pointcut="within(com.foo.bar..*)"
    		throwing="exception" method="processError"/>
        </aop:aspect>
    </aop:config>
    The bean that throws the exception is created by Spring - it is created as part of a JUnit test class and Spring injects an instance of the bean via AbstractTransactionalDataSourceSpringContextTests.

    Thanks

  6. #6
    Join Date
    Jan 2008
    Posts
    4

    Default

    Aha...I found my problem. The exception I was throwing never crossed the proxy so it was not picked up by my error handler. I changed my test code to throw a runtime exception, instead of catching it inline and it worked.

    Thanks for the help

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    doh.... That I missed that, complete missed your try/catch block.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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