mirror of
https://github.com/penpot/penpot.git
synced 2026-08-01 10:56:20 +00:00
✨ Improve nitrate module JSON handling and error management
This commit is contained in:
parent
46f50aab16
commit
acc383ba31
@ -1,6 +1,7 @@
|
|||||||
(ns app.nitrate
|
(ns app.nitrate
|
||||||
"Module that make calls to the external nitrate aplication"
|
"Module that make calls to the external nitrate aplication"
|
||||||
(:require
|
(:require
|
||||||
|
[app.common.json :as json]
|
||||||
[app.common.logging :as l]
|
[app.common.logging :as l]
|
||||||
[app.common.schema :as sm]
|
[app.common.schema :as sm]
|
||||||
[app.common.schema.generators :as sg]
|
[app.common.schema.generators :as sg]
|
||||||
@ -9,7 +10,6 @@
|
|||||||
[app.http.client :as http]
|
[app.http.client :as http]
|
||||||
[app.rpc :as-alias rpc]
|
[app.rpc :as-alias rpc]
|
||||||
[app.setup :as-alias setup]
|
[app.setup :as-alias setup]
|
||||||
[app.util.json :as json]
|
|
||||||
[clojure.core :as c]
|
[clojure.core :as c]
|
||||||
[integrant.core :as ig]))
|
[integrant.core :as ig]))
|
||||||
|
|
||||||
@ -27,7 +27,7 @@
|
|||||||
"x-profile-id" (str profile-id)}
|
"x-profile-id" (str profile-id)}
|
||||||
:uri uri
|
:uri uri
|
||||||
:version :http1.1}
|
:version :http1.1}
|
||||||
(= method :post) (assoc :body (json/encode request-params))))))
|
(= method :post) (assoc :body (json/encode request-params :key-fn json/write-camel-key))))))
|
||||||
|
|
||||||
(defn- with-retries
|
(defn- with-retries
|
||||||
[handler max-retries]
|
[handler max-retries]
|
||||||
@ -49,15 +49,27 @@
|
|||||||
|
|
||||||
(defn- with-validate [handler uri schema]
|
(defn- with-validate [handler uri schema]
|
||||||
(fn []
|
(fn []
|
||||||
(let [coercer-http (sm/coercer schema
|
(let [response (handler)
|
||||||
:type :validation
|
status (:status response)]
|
||||||
:hint (str "invalid data received calling " uri))]
|
(if (>= status 400)
|
||||||
(try
|
;; For error status codes (4xx, 5xx), fail immediately without validation
|
||||||
(coercer-http (-> (handler) :body json/decode))
|
(do
|
||||||
(catch Exception e
|
(l/error :hint "nitrate request failed with error status"
|
||||||
;; TODO Error handling
|
:uri uri
|
||||||
(l/error :hint "error validating json response" :cause e)
|
:status status
|
||||||
nil)))))
|
:body (:body response))
|
||||||
|
nil)
|
||||||
|
;; For success status codes, validate the response
|
||||||
|
(let [coercer-http (sm/coercer schema
|
||||||
|
:type :validation
|
||||||
|
:hint (str "invalid data received calling " uri))
|
||||||
|
data (-> response :body (json/decode :key-fn json/read-kebab-key))]
|
||||||
|
(try
|
||||||
|
(coercer-http data)
|
||||||
|
(catch Exception e
|
||||||
|
;; TODO Error handling
|
||||||
|
(l/error :hint "error validating json response" :cause e)
|
||||||
|
nil)))))))
|
||||||
|
|
||||||
(defn- request-to-nitrate
|
(defn- request-to-nitrate
|
||||||
[cfg method uri schema {:keys [::rpc/profile-id request-params] :as params}]
|
[cfg method uri schema {:keys [::rpc/profile-id request-params] :as params}]
|
||||||
@ -84,13 +96,14 @@
|
|||||||
[:map
|
[:map
|
||||||
[:id ::sm/uuid]
|
[:id ::sm/uuid]
|
||||||
[:name ::sm/text]
|
[:name ::sm/text]
|
||||||
[:slug ::sm/text]])
|
[:slug ::sm/text]
|
||||||
|
[:is-your-penpot :boolean]])
|
||||||
|
|
||||||
(def ^:private schema:team
|
(def ^:private schema:team
|
||||||
[:map
|
[:map
|
||||||
[:id ::sm/uuid]
|
[:id ::sm/uuid]
|
||||||
[:organizationId ::sm/uuid]
|
[:organization-id ::sm/uuid]
|
||||||
[:yourPenpot :boolean]])
|
[:is-your-penpot :boolean]])
|
||||||
|
|
||||||
;; TODO Unify with schemas on backend/src/app/http/management.clj
|
;; TODO Unify with schemas on backend/src/app/http/management.clj
|
||||||
(def ^:private schema:timestamp
|
(def ^:private schema:timestamp
|
||||||
@ -176,8 +189,8 @@
|
|||||||
(defn- set-team-org
|
(defn- set-team-org
|
||||||
[cfg {:keys [organization-id team-id is-default] :as params}]
|
[cfg {:keys [organization-id team-id is-default] :as params}]
|
||||||
(let [baseuri (cf/get :nitrate-backend-uri)
|
(let [baseuri (cf/get :nitrate-backend-uri)
|
||||||
params (assoc params :request-params {:teamId team-id
|
params (assoc params :request-params {:team-id team-id
|
||||||
:yourPenpot (true? is-default)})]
|
:is-your-penpot (true? is-default)})]
|
||||||
(request-to-nitrate cfg :post
|
(request-to-nitrate cfg :post
|
||||||
(str baseuri
|
(str baseuri
|
||||||
"/api/organizations/"
|
"/api/organizations/"
|
||||||
@ -246,7 +259,7 @@
|
|||||||
:organization-id (:id org)
|
:organization-id (:id org)
|
||||||
:organization-name (:name org)
|
:organization-name (:name org)
|
||||||
:organization-slug (:slug org)
|
:organization-slug (:slug org)
|
||||||
:is-default (or (:is-default team) (true? (:isYourPenpot org))))
|
:is-default (or (:is-default team) (true? (:is-your-penpot org))))
|
||||||
team))
|
team))
|
||||||
(catch Throwable cause
|
(catch Throwable cause
|
||||||
(l/error :hint "failed to get team organization info"
|
(l/error :hint "failed to get team organization info"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user