Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a global exception handling in Raku Cro Application

Tags:

raku

cro

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?

like image 782
user13195651 Avatar asked Sep 30 '21 04:09

user13195651


People also ask

How do I create a global exception handler?

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.

Why do we need global exception handler?

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.

What is global exception handling in C#?

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.

How many global exception handlers are there?

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.


1 Answers

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.
}
like image 179
Jonathan Worthington Avatar answered Oct 18 '22 09:10

Jonathan Worthington