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:
parent
b194c0c5d8
commit
1e0dacfe9b
2 changed files with 14 additions and 17 deletions
|
@ -9,11 +9,11 @@
|
||||||
(:require
|
(:require
|
||||||
[app.common.exceptions :as ex]
|
[app.common.exceptions :as ex]
|
||||||
[app.common.logging :as l]
|
[app.common.logging :as l]
|
||||||
|
[app.common.spec :as us]
|
||||||
[app.common.uuid :as uuid]
|
[app.common.uuid :as uuid]
|
||||||
[clojure.pprint]
|
[clojure.pprint]
|
||||||
[clojure.spec.alpha :as s]
|
[clojure.spec.alpha :as s]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]))
|
||||||
[expound.alpha :as expound]))
|
|
||||||
|
|
||||||
(defn- parse-client-ip
|
(defn- parse-client-ip
|
||||||
[{:keys [headers] :as request}]
|
[{:keys [headers] :as request}]
|
||||||
|
@ -41,9 +41,7 @@
|
||||||
:frontend-version (get headers "x-frontend-version" "unknown")})
|
:frontend-version (get headers "x-frontend-version" "unknown")})
|
||||||
|
|
||||||
(when (and data (::s/problems data))
|
(when (and data (::s/problems data))
|
||||||
{:spec-explain (binding [s/*explain-out* expound/printer]
|
{:spec-explain (us/pretty-explain data)}))))
|
||||||
(with-out-str
|
|
||||||
(s/explain-out (update data ::s/problems #(take 10 %)))))}))))
|
|
||||||
|
|
||||||
(defmulti handle-exception
|
(defmulti handle-exception
|
||||||
(fn [err & _rest]
|
(fn [err & _rest]
|
||||||
|
@ -59,20 +57,10 @@
|
||||||
[err _]
|
[err _]
|
||||||
{:status 400 :body (ex-data 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
|
(defmethod handle-exception :validation
|
||||||
[err _]
|
[err _]
|
||||||
(let [data (ex-data err)
|
(let [data (ex-data err)
|
||||||
explain (explain-spec-error-data data)]
|
explain (us/pretty-explain data)]
|
||||||
{:status 400
|
{:status 400
|
||||||
:body (-> data
|
:body (-> data
|
||||||
(dissoc ::s/problems)
|
(dissoc ::s/problems)
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
[app.common.exceptions :as ex]
|
[app.common.exceptions :as ex]
|
||||||
[app.common.uuid :as uuid]
|
[app.common.uuid :as uuid]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
[expound.alpha]))
|
[expound.alpha :as expound]))
|
||||||
|
|
||||||
(s/check-asserts true)
|
(s/check-asserts true)
|
||||||
|
|
||||||
|
@ -268,3 +268,12 @@
|
||||||
(spec-assert* ~spec params# ~message mdata#)
|
(spec-assert* ~spec params# ~message mdata#)
|
||||||
(apply origf# params#)))))))
|
(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 %))))))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue