0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-24 15:39:50 -05:00

Add reusable helper for expound pretty printing

This commit is contained in:
Andrey Antukh 2022-02-02 23:50:09 +01:00 committed by Alonso Torres
parent b194c0c5d8
commit 1e0dacfe9b
2 changed files with 14 additions and 17 deletions

View file

@ -9,11 +9,11 @@
(:require
[app.common.exceptions :as ex]
[app.common.logging :as l]
[app.common.spec :as us]
[app.common.uuid :as uuid]
[clojure.pprint]
[clojure.spec.alpha :as s]
[cuerdas.core :as str]
[expound.alpha :as expound]))
[cuerdas.core :as str]))
(defn- parse-client-ip
[{:keys [headers] :as request}]
@ -41,9 +41,7 @@
:frontend-version (get headers "x-frontend-version" "unknown")})
(when (and data (::s/problems data))
{:spec-explain (binding [s/*explain-out* expound/printer]
(with-out-str
(s/explain-out (update data ::s/problems #(take 10 %)))))}))))
{:spec-explain (us/pretty-explain data)}))))
(defmulti handle-exception
(fn [err & _rest]
@ -59,20 +57,10 @@
[err _]
{:status 400 :body (ex-data err)})
(defn- explain-spec-error-data
[data]
(when (and (::s/problems data)
(::s/value data)
(::s/spec data))
(binding [s/*explain-out* expound/printer]
(with-out-str
(s/explain-out (update data ::s/problems #(take 10 %)))))))
(defmethod handle-exception :validation
[err _]
(let [data (ex-data err)
explain (explain-spec-error-data data)]
explain (us/pretty-explain data)]
{:status 400
:body (-> data
(dissoc ::s/problems)

View file

@ -18,7 +18,7 @@
[app.common.exceptions :as ex]
[app.common.uuid :as uuid]
[cuerdas.core :as str]
[expound.alpha]))
[expound.alpha :as expound]))
(s/check-asserts true)
@ -268,3 +268,12 @@
(spec-assert* ~spec params# ~message mdata#)
(apply origf# params#)))))))
(defn pretty-explain
([data] (pretty-explain data nil))
([data {:keys [max-problems] :or {max-problems 10}}]
(when (and (::s/problems data)
(::s/value data)
(::s/spec data))
(binding [s/*explain-out* expound/printer]
(with-out-str
(s/explain-out (update data ::s/problems #(take max-problems %))))))))