Fork me on GitHub
#reitit
<
2019-02-16
>
colliderwriter02:02:40

I got this to work in the end by hacking together a two-parameter version of reitit.http.coercion/coerce-exceptions-interceptor which returns what i wanted but that doesn't seem like what the official answer ought to be. The arity exception makes me think this is a pedestal / sieppari interceptor mismatch but I can't figure out what I was supposed to use

colliderwriter02:02:58

I'd love to do this the right way (and know where to look in the future) so for reference, here's the hack

colliderwriter02:02:13

(defn my-coerce-exceptions-interceptor
  "Interceptor for handling coercion exceptions.
    Expects a :coercion of type `reitit.coercion/Coercion`
    and :parameters or :responses from route data, otherwise does not mount."
  []
  {:name ::coerce-exceptions
   :compile (fn [{:keys [coercion parameters responses]} _]
              (if (and coercion (or parameters responses))
                {:error (fn [ctx ex]
                          (let [data (ex-data (or (:error ctx)
                                                  ex))]
                            (if-let [status
                                     (case (:type data)
                                       :reitit.coercion/request-coercion 400
                                       :reitit.coercion/response-coercion 500
                                       nil)]
                              (let [response
                                    {:status status,
                                     :body nil
                                     #_["coercion error"]
                                     #_(coercion/encode-error data)}]
                                (-> ctx
                                    (assoc :response response)
                                    (assoc :error nil)))
                              ctx)))}))})

ikitommi13:02:25

@colliderwriter the arity-error is a pedestal/sieppari thing and documented here: https://metosin.github.io/reitit/http/pedestal.html#compatibility.

ikitommi13:02:51

now that there is a reitit-pedestal module, I think we could ship a pedestal-compatible coercion exception interceptor there. Also, a version of reitit.http.interceptors.exception/exception-interceptor that works with pedestal.

ikitommi13:02:45

I think your example is quite close how it should work. would you like to do a PR to include those?