Fork me on GitHub
Kira McLean03:11:43

I was actually just doing exactly this today at work.. small world! I started with the [examples here]( and ended up with something roughy like this, which seemed to do the trick for me:

(ns some.ns
  (:require [reitit.ring :as ring]
            [reitit.ring.middleware.exception :as exception]
            [reitit.ring.coercion :as coercion]
            [reitit.coercion.malli :as mc]
            [reitit.coercion :as rc]]))

(defn- coercion-handler [status]
  (fn [e _]
    {:status status
     :body ,,, your custom error message here ,,,)}))

(def ^:private exception-middleware
    {::rc/request-coercion (coercion-handler 400)
     ::rc/response-coercion (coercion-handler 500)})))

(ring/router data
               {:data {:coercion mc/coercion
                       :middleware [,,, bunch of other middlewares,,,
                                    exception-middleware ;; <--- custom exception handling
                                    coercion/coerce-response-middleware ;; coercing response bodies
                                    coercion/coerce-request-middleware ;; coercing request parameters
                                    ,,, more middlewares ,,,

thanks 1

to avoid fn we can use partial

(defn- coercion-handler [status]
  (fn [e _]

Björn Ebbinghaus12:11:19

Hi. I have the following routes:

[["/my-context/{context}" ::context
  ["" {:name ::context-default, :segment ["default"]}]
  ["/something/{foo}" ::something]]]
And I want to add some additional data (if not present) so that in the end I get:
  {:name ::context-default, 
   :segment ["default"], 
   :segment-route ["my-context" :context "default"]}]
  {:name ::something, 
   :segment ["something" :foo], 
   :segment-route ["my-context" :context "something" :foo]}]]
Is this possible within reitit router arguments? Expand doesn't have access to the route itself.

Björn Ebbinghaus12:11:26

I suspect, that I have to preprocess my routes by myself.