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?