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

**URL:** <https://discourse.julialang.org/t/how-to-send-a-response-at-arbitrary-code-points-in-genie-route/103766>\
**Category:** General Usage\
**Tags:** genie\
**Created:** [September 12, 2023, 2:32am UTC](https://discourse.julialang.org/t/how-to-send-a-response-at-arbitrary-code-points-in-genie-route/103766 "2023-09-12T02:32:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Boik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_boik/32/33654_2.png) [@John\_Boik](https://discourse.julialang.org/u/John_Boik)\
**Post date:** [September 12, 2023, 2:32am UTC](https://discourse.julialang.org/t/how-to-send-a-response-at-arbitrary-code-points-in-genie-route/103766/1 "2023-09-12T02:32:29Z")

</div>

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:

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

```

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![gvdr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gvdr/32/6387_2.png) [@gvdr](https://discourse.julialang.org/u/gvdr)\
**Post date:** [September 12, 2023, 2:43am UTC](https://discourse.julialang.org/t/how-to-send-a-response-at-arbitrary-code-points-in-genie-route/103766/2 "2023-09-12T02:43:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![John\_Boik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_boik/32/33654_2.png) [@John\_Boik](https://discourse.julialang.org/u/John_Boik)\
**Post date:** [September 12, 2023, 3:44am UTC](https://discourse.julialang.org/t/how-to-send-a-response-at-arbitrary-code-points-in-genie-route/103766/3 "2023-09-12T03:44:35Z")

</div>

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.
