[Coldbox 7.3.1] Can You Catch an Exception in Handler Around Advice Triggered in Pre Advice?

In the majority of my Coldbox apps, I have a BaseHandler Around Advice aroundHandler() that wraps all events. This gives me a convenient way to catch specific exception types, like BadRequest, NotFound, EntityNotFound, ValidationException, etc…

However, I discovered something today, that I’m not sure is by design, or a bug. If I use a handler Pre Advice, and throw an exception that would normally be caught by the aroundHandler() it does not, and goes straight to Whoops.

So my question is this, can aroundHandler() be used to catch an exception that is thrown in a Pre Advice? Or do Pre Advices get called independently and outside of any Around Advices?

From the docs:

Blockquote
This will allow you to run both before and after advices but also surround the method call with whatever logic you want like transactions , try/catch blocks, locks or even decide to not execute the action at all

Here’s an sample aroundHandler I like to use:
https://gist.github.com /homestar9/15de3fb4dc8f5fa076813f534b1c3ea9

Here’s a sample Pre Advice method:
https://gist.github.com /homestar9/902add3d2043ccd5d87e5342c0537cbf

Note: The form wouldn’t let me post these code samples for some reason (403 errors) so you’ll need to fix the links yourself.

in answer to your question, i think your hunch is correct, the Pre and Post run outside of the AroundHandler so they would not be caught

Coldbox Code Chunk

However, I have been down this road with validation. you may be able to make your own preValidationCreate advice and copy the RestBaseHandler to make your own where you could call your custom function (if it exists) to run validation in the around handler.

just an idea.

1 Like

Thank you, @scott_steinbeck! I believe you are right and I like your creative idea. I’ll play around with it and if I come up with a solution I will post here for anyone interested.