mirror of
https://github.com/penpot/penpot.git
synced 2025-05-12 23:16:37 +02:00
🐛 Properly handle errors on body parsing middleware.
This commit is contained in:
parent
01c89f6554
commit
224aa5b89a
2 changed files with 24 additions and 30 deletions
|
@ -54,7 +54,7 @@
|
||||||
{:status 400 :body (ex-data err)})
|
{:status 400 :body (ex-data err)})
|
||||||
|
|
||||||
(defmethod handle-exception :validation
|
(defmethod handle-exception :validation
|
||||||
[err req]
|
[err _]
|
||||||
(let [edata (ex-data err)]
|
(let [edata (ex-data err)]
|
||||||
{:status 400
|
{:status 400
|
||||||
:body (dissoc edata ::s/problems)}))
|
:body (dissoc edata ::s/problems)}))
|
||||||
|
|
|
@ -34,38 +34,32 @@
|
||||||
(t/read! reader)))
|
(t/read! reader)))
|
||||||
|
|
||||||
(parse-json [body]
|
(parse-json [body]
|
||||||
(json/read body))
|
(json/read body))]
|
||||||
|
|
||||||
(parse [type body]
|
|
||||||
(try
|
|
||||||
(case type
|
|
||||||
:json (parse-json body)
|
|
||||||
:transit (parse-transit body))
|
|
||||||
(catch Exception e
|
|
||||||
(let [data {:type :parse
|
|
||||||
:hint "unable to parse request body"
|
|
||||||
:message (ex-message e)}]
|
|
||||||
{:status 400
|
|
||||||
:headers {"content-type" "application/transit+json"}
|
|
||||||
:body (t/encode-str data {:type :json-verbose})}))))]
|
|
||||||
|
|
||||||
(fn [{:keys [headers body] :as request}]
|
(fn [{:keys [headers body] :as request}]
|
||||||
(let [ctype (get headers "content-type")]
|
(try
|
||||||
(handler
|
(let [ctype (get headers "content-type")]
|
||||||
(case ctype
|
(handler (case ctype
|
||||||
"application/transit+json"
|
"application/transit+json"
|
||||||
(let [params (parse :transit body)]
|
(let [params (parse-transit body)]
|
||||||
(-> request
|
(-> request
|
||||||
(assoc :body-params params)
|
(assoc :body-params params)
|
||||||
(update :params merge params)))
|
(update :params merge params)))
|
||||||
|
|
||||||
"application/json"
|
"application/json"
|
||||||
(let [params (parse :json body)]
|
(let [params (parse-json body)]
|
||||||
(-> request
|
(-> request
|
||||||
(assoc :body-params params)
|
(assoc :body-params params)
|
||||||
(update :params merge params)))
|
(update :params merge params)))
|
||||||
|
|
||||||
request))))))
|
request)))
|
||||||
|
(catch Exception e
|
||||||
|
(let [data {:type :validation
|
||||||
|
:code :unable-to-parse-request-body
|
||||||
|
:hint "malformed params"}]
|
||||||
|
(l/error :hint (ex-message e) :cause e)
|
||||||
|
{:status 400
|
||||||
|
:headers {"content-type" "application/transit+json"}
|
||||||
|
:body (t/encode-str data {:type :json-verbose})}))))))
|
||||||
|
|
||||||
(def parse-request-body
|
(def parse-request-body
|
||||||
{:name ::parse-request-body
|
{:name ::parse-request-body
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue