How to send a response at arbitrary code points in Genie route?

I’m trying to use Genie.jl for a fairly simple REST API. Given a route, I would like to be able to send a response at arbitrary code points, depending on conditions. For example, if an exception is raised part way through the code, I would like to send a response with an error message. But apparently, the response has to be the last line of code in the route. How do you send a response at arbitrary points?

For example, this will send the default response:

route("/test") do
    json("default response")
end 

But this will not send the error message; it sends the default response instead:

route("/test") do
    # suppose some error occurs and I want to respond with a message
    if true
        json("error message")
    end
    
    # if there is an error, there is code here that I do not want to run

    # if there is no error, then send default response
    json("default response")
end 

I suppose I could have a long if-elseif-else, where each elseif sends an early response if a condition is true, but that would be awkward to code, especially if each elseif contained complex code. Is there a better way?

Probably the easiest way is to wrap the logic you want to get executed into a function. In that way, the route will return what the function returns.

Thanks much. That will work, but its kind of a pain if the error message is generated within a function that calls a function that calls a function… Then, the logic has to propagate the message up several levels before it reaches the outer route function.