Fork me on GitHub
#malli
<
2022-02-10
>
Zaymon05:02:27

Hello. Is there a way to make the exception-info created by Malli coercion less verbose? Currently 500 lines of output to the terminal is a bit hard to parse, mostly because the exception info includes a lot of information about the request. Either that or a better way to print only the relevant parts of the exception would also be appreciated.

Zaymon05:02:34

(def malli-coercer
  (malli-coercion/create
   {;; set of keys to include in error messages
    :error-keys #{:humanized
              ;;     :type
              ;;     :coercion
              ;;     :in
                  :schema
                  :value
                  :errors
                  #_:transformed}
   ;; schema identity function (default: close all map schemas)
    :compile mu/closed-schema
   ;; validate request & response
    :validate true
   ;; top-level short-circuit to disable request & response coercion
    :enabled true
   ;; malli options
    :options nil
    :muuntaja formats/instance}))
My Coercion is setup like this

emccue19:02:26

malli/humanize?

Zaymon01:02:40

Not sure how to apply that to exception-info

jussi12:02:29

Pondered the same the other day, ended up with simplistic helpers

(defn- path->str
  [path]
  (string/join " -> " (map #(if (number? %)
                              (str "[" % "]")
                              (name %))
                           path)))

(defn pretty-schema-error
  "Make schema errors a bit more readable. Handles nil's and formats path."
  [error]
  (let [path (:path error)
        type (:type error)]
    {:path (path->str path)
     :type (if (nil? type) "nil" (name type))})) 
Using it in our reitit API when handling :reitit.coercion/request-coercion errors for example.