Fork me on GitHub
#clojure
<
2024-01-22
>
Samuel Ludwig17:01:44

Posting here from the (rather unpopulated) #compojure channel: In compojure, am I able to create a "catch-all" default route if no other routes are matched? I'm basically trying to return a response that includes parts of the original request (for debugging). There is the compojure.route/not-found function(macro?), but I don't see how I'd be able to get the request with it- as far as I can tell I can only provide an explicit (as in, non-parameterized) body to use as response.

hiredman17:01:18

The way compojure does routing it applies a handler to a request, and if the handler returns nil that means the route did not match

hiredman17:01:18

So (fn [req] (or (handler req) (my-not-found-handler req)))

hiredman17:01:33

Alternatively, I believe you can just put a ring handler at the end of all your routes

hiredman17:01:30

Because what (GET ...) actually does is create a ring handler, so what routes (and defroutes) take is a sequence of handlers to try in turn

Samuel Ludwig17:01:30

I think I understand what you're talking about, I am defining everything via defroutes, I'll try and wrap that in something that'll give me my debug info- appreciate the input so far!

hiredman17:01:52

Or just put your not found handler at the end

hiredman17:01:43

(defroutes .... (fn [req] not found stuff))

hiredman17:01:19

The not-found route thing in compojure isn't for handling not found routes, it is for returning 404 not found, which you want to do in some cases if no routes match