I am working with a rather larger Cro application with dozens of routes, models and other logic. At the moment in each route block is a CATCH
to handle exception. That is not much maintenance friendly, not to speak of the work to add them.
So, I was wondering if its a better way to do that. One CATCH
handler in the main route block does not work. The exceptions are only caught in the route block where they are thrown. Threading issue probably.
Is there one place where I can implement an exception handler which gets all exceptions and can handle them without causing the application to die?
Adding a Global Exception Handler The New Global Handler window opens. Type in a Name for the handler and save it in the project path. Click Create, a Global Exception Handler is added to the automation project.
A global solution would handle all the unhandled exceptions, construct a meaningful response, and return a meaningful response to the users. So a global exception handler is used to catch all the unhandled exceptions.
An ExceptionFilterAttribute is used to collect unhandled exceptions. You can register it as a global filter, and it will function as a global exception handler. Another option is to use a custom middleware designed to do nothing but catch unhandled exceptions.
Only one Global Exception Handler can be set per automation project. By default, the template retries the exception 3 times before aborting the execution. This default behavior can be changed from inside the template.
You can use the around
function in your route
block to specify something that wraps around all of the route handlers. The documentation of around
gives an example of using it to handle exceptions thrown by all route handlers in the route
block (repeated here for convenience):
my $application = route {
around -> &handler {
# Invoke the route handler
handler();
CATCH {
# If any handler produces this exception...
when Some::Domain::Exception::UpdatingOldVersion {
# ...return a HTTP 409 Conflict response.
conflict;
}
}
}
# Put your get, post, etc. here.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With