Fork me on GitHub
#reitit
<
2021-03-24
>
wombawomba18:03:37

So I'm using Reitit together with Schema for validation, and I want a subset of my routes to return a generic 404 page rather than the validation error when given bad inputs. How can I make this happen?

ikitommi18:03:06

@wombawomba you can mount an custom exception handling middleware for those routes: catch the coercion error and return 404 instead.

wombawomba18:03:41

@ikitommi sounds good, thanks.. how would I do that again? 🙂

juhoteperi19:03:56

Something like this:

(def my-ex-handler (exception/create-exception-middleware {:my-ex-type (fn [message ex req] ...) or.my.ExClass (fn [... ] ...)}))

["/" {:middleware [my-ex-handler]} ["/foo" { ...}] ["/bar"]]
So https://cljdoc.org/d/metosin/reitit/0.5.12/doc/ring/exception-handling-with-ring but instead of adding your exception handlers to default-handlers, only handle some types and it should let the other exceptions to flow to top level handler.

juhoteperi19:03:56

Something like this:

(def my-ex-handler (exception/create-exception-middleware {:my-ex-type (fn [message ex req] ...) or.my.ExClass (fn [... ] ...)}))

["/" {:middleware [my-ex-handler]} ["/foo" { ...}] ["/bar"]]
So https://cljdoc.org/d/metosin/reitit/0.5.12/doc/ring/exception-handling-with-ring but instead of adding your exception handlers to default-handlers, only handle some types and it should let the other exceptions to flow to top level handler.

wombawomba20:03:18

how do I add middleware to a collection of 'flat' routes? I want to do something like the following, but I can't get it to work:

(reitit.ring/router
 [[""
   {:middleware [my-middleware]}
   ["/foo" {...}]
   ["/bar" {...}]]
  ["/api"
   ...]])

wombawomba20:03:52

oh okay, that actually works, it's just that my middleware ends up in the wrong end of the middleware stack

wombawomba20:03:24

how do I get it to go before the schema validation?

wombawomba20:03:58

would I need to add a transform to the router?

wombawomba20:03:29

okay yeah doing that worked 🙂