Fork me on GitHub
#reitit
<
2021-11-06
>
Kira McLean03:11:43

I was actually just doing exactly this today at work.. small world! I started with the [examples here](https://cljdoc.org/d/metosin/reitit/0.5.15/doc/ring/exception-handling-with-ring) 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
  (exception/create-exception-middleware
   (merge
    exception/default-handlers
    {::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
papachan15:11:11

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:
[["/my-context/{context}" 
  {:name ::context-default, 
   :segment ["default"], 
   :segment-route ["my-context" :context "default"]}]
 ["/my-context/{context}/something/{foo}" 
  {: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.