From matt at new-bamboo.co.uk Tue Mar 25 22:26:08 2008 From: matt at new-bamboo.co.uk (Matthew Ford) Date: Wed, 26 Mar 2008 02:26:08 +0000 Subject: [Aquarium-users] Advise that catches one type of exception and raises another Message-ID: <4b542e970803251926p7a062e10qd09f5eeac4331977@mail.gmail.com> Hi, Im trying to write an aspect which catches one error and then throws another, but im having some difficulty Here is my aspect (pointcut on methods in my controller): Aspect.new :after_raising => DataMapper::ObjectNotFoundError, :pointcut => @pointcut do |join_point, object, *args| object rescue DataMapper::ObjectNotFoundError raise Exceptions::NotFound end Im trying to avoid this exception, Exception raised while executing "before" advice for "Users#show". Any help would be greatly appreciated thanks, Matt -- Matthew Ford New Bamboo Web Development Ltd. Creating fresh, flexible and fast-growing web applications is our passion. Tel. +44 (0)20 7099 7486 Mob.+44 (0)78 8212 4376 E-mail. matt at new-bamboo.co.uk http://www.new-bamboo.co.uk From dean at aspectprogramming.com Wed Mar 26 11:00:11 2008 From: dean at aspectprogramming.com (Dean Wampler) Date: Wed, 26 Mar 2008 10:00:11 -0500 Subject: [Aquarium-users] Advise that catches one type of exception and raises another In-Reply-To: <4b542e970803251926p7a062e10qd09f5eeac4331977@mail.gmail.com> References: <4b542e970803251926p7a062e10qd09f5eeac4331977@mail.gmail.com> Message-ID: Matthew, By "some difficulty", I assume that you don't get the exception rescuing you expect, but the original exception gets raised. True? You have to use :around advice for exception "wrapping" like this. The reason is that :after and :after_raising will still raise the original exception. Only around advice is designed to change the control flow. Here's the idiom you want (and I'll add an example of this in the next minor release - next week?) Aquarium::Aspects::Aspect.new :around, :pointcut => @pointcut do | join_point, obj, *args| begin jp.proceed # call the wrapped join point rescue DataMapper::ObjectNotFoundError => e raise Exceptions::NotFound end end It may be confusing that you can't handle the exception in :after_raising advice. I followed the AspectJ model. It would also be confusing if this type of after advice let you circumvent the control flow while other types of advice (except for around) didn't allow this. However, what you tried to do is so natural that I think it would be nice to have a special type of advice for this purpose. I posted a feature request, #19119 for this purpose: Provide an "after_raising" type of advice that lets you handle the exception. Feel free to comment. A possibility might be ":rescue", so your example would become: Aquarium::Aspects::Aspect.new :rescue => DataMapper::ObjectNotFoundError, :pointcut => @pointcut do | join_point, obj, *args| raise Exceptions::NotFound end Yea, that's a lot simpler, isn't it?? Finally, at first, I was a little suspicious of storing the pointcut in an attribute "@pointcut". The aspect is created as it's parsed (like a method definition). Is it possible this attribute is not initialized at that time? Also, I didn't understand the first line in the advice: > object rescue DataMapper::ObjectNotFoundError Was that supposed to be a comment? HTH, dean On Mar 25, 2008, at 9:26 PM, Matthew Ford wrote: > Hi, > > Im trying to write an aspect which catches one error and then throws > another, but im having some difficulty > > Here is my aspect (pointcut on methods in my controller): > Aspect.new :after_raising => DataMapper::ObjectNotFoundError, > :pointcut => @pointcut do |join_point, object, *args| > object rescue DataMapper::ObjectNotFoundError > raise Exceptions::NotFound > end > > Im trying to avoid this exception, Exception raised while executing > "before" advice for "Users#show". > > Any help would be greatly appreciated thanks, > Matt > > -- > Matthew Ford > > New Bamboo Web Development Ltd. > Creating fresh, flexible and fast-growing web applications is our > passion. > > Tel. +44 (0)20 7099 7486 > Mob.+44 (0)78 8212 4376 > E-mail. matt at new-bamboo.co.uk > http://www.new-bamboo.co.uk > _______________________________________________ > Aquarium-users mailing list > Aquarium-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/aquarium-users Dean Wampler, Ph.D. dean at objectmentor.com http://www.objectmentor.com See also: http://www.aspectprogramming.com AOP advocacy site http://aquarium.rubyforge.org AOP for Ruby http://www.contract4j.org Design by Contract for Java5 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/aquarium-users/attachments/20080326/487d6c1b/attachment.html