reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
lispyclouds 2021-03-07T20:49:36.033700Z

Hello! I'm using reitit with malli coercion and currently getting errors like this whenever coercion fails:

{
    "message": "Request coercion failed: #reitit.coercion.CoercionError{:schema [:map {:closed false} [:url string?]], :value {:invalid-key &amp;quot;<http://resource:8000>&amp;quot;}, :errors (#Error{:path [:url], :in [:url], :schema [:map {:closed false} [:url string?]], :type :malli.core/missing-key}), :transformed {:invalid-key &amp;quot;<http://resource:8000>&amp;quot;}}",
    "url": "/resource-providers/resource-git",
    "status": "500"
}
along with the status as 500 instead of 400 as I would like. This is my data config:
{:data {:coercion     malli/coercion
                         :muuntaja     m/instance
                         :interceptors [(parameters/parameters-interceptor)
                                        (muuntaja/format-negotiate-interceptor)
                                        (muuntaja/format-response-interceptor)
                                        (muuntaja/format-request-interceptor)
                                        (coercion/coerce-response-interceptor)
                                        (coercion/coerce-request-interceptor)
                                        (coercion/coerce-exceptions-interceptor)]}}
Is there a way to prettify/humanize these errors like the examples of spec on the site? and how do i make it return 400? Thanks! 😄

Matheus Moreira 2021-03-09T13:43:10.000300Z

Hello. I have an app with a similar setup and I use an exception middleware; maybe that could help in your case:

(def exception-middleware
  (reitit.ring.middleware.exception/create-exception-middleware
    (merge
      reitit.ring.middleware.exception/default-handlers
      {:reitit.ring.middleware.exception/default default-exception-handler})))

:middleware [...
             muuntaja/format-negotiate-middleware
             muuntaja/format-response-middleware
             exception-middleware
             muuntaja/format-request-middleware
             ...]

Matheus Moreira 2021-03-09T13:44:48.000600Z

Check the reitit.ring.middleware.exception/default-handlers to discover the keys you want to override. In my case it was the default-exception-handler because I wanted to add to the body of the response the exception’s message.

willier 2021-03-10T01:06:04.011500Z

(malli/create {:error-keys #{:humanized}}) for the :coercion key above

willier 2021-03-10T01:07:06.012700Z

instead of malli/coercion as u have

lispyclouds 2021-03-10T06:44:59.013Z

Hey @willier thanks, i tried this

(ns main
  (:require [muuntaja.core :as m]
            [reitit.ring :as ring]
            [reitit.http :as http]
            [reitit.coercion.malli :as malli]
            [reitit.http.coercion :as coercion]
            [reitit.http.interceptors.parameters :as parameters]
            [reitit.http.interceptors.muuntaja :as muuntaja]
            [reitit.interceptor.sieppari :as sieppari]
            [ring.adapter.jetty :as jetty]))

(def routes
  [["/add/{n1}/{n2}"
    {:get
     {:handler (fn [{{{:keys [n1 n2]} :path} :parameters}]
                {:status 200
                 :body   {:message (+ n1 n2)}})
      :parameters
      {:path
       [:map
        [:n1 int?]
        [:n2 int?]]}}}]])

(def server
  (http/ring-handler
    (http/router routes
                 {:data {:coercion     (malli/create {:error-keys #{:humanized}})
                         :muuntaja     m/instance
                         :interceptors [(parameters/parameters-interceptor)
                                        (muuntaja/format-negotiate-interceptor)
                                        (muuntaja/format-response-interceptor)
                                        (muuntaja/format-request-interceptor)
                                        (coercion/coerce-response-interceptor)
                                        (coercion/coerce-request-interceptor)]}})
    (ring/routes
      (ring/create-default-handler
        {:not-found (constantly {:status  404
                                 :headers {"Content-Type" "application/json"}
                                 :body    "{\"message\": \"Took a wrong turn?\"}"})}))
    {:executor sieppari/executor}))

(comment
  (def s
    (jetty/run-jetty (var server)
                     {:host   "0.0.0.0"
                      :port   7777
                      :join?  false
                      :async? true}))

  (.stop s))
still get
http <http://localhost:7777/add/1/aa> Accept:application/json
HTTP/1.1 500 Server Error
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 316
Content-Type: application/json
Server: Jetty(9.4.36.v20210114)

{
    "message": "Request coercion failed: #reitit.coercion.CoercionError{:schema [:map {:closed true} [:n1 int?] [:n2 int?]], :value {:n1 1, :n2 &amp;quot;aa&amp;quot;}, :errors (#Error{:path [:n2], :in [:n2], :schema int?, :value &amp;quot;aa&amp;quot;}), :transformed {:n1 1, :n2 &amp;quot;aa&amp;quot;}}",
    "status": "500",
    "url": "/add/1/aa"
}
Am i missing/using some interceptors wrongly?