0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 07:50:43 -05:00

📎 Add service result wrapper

Allows attach metadata to values that does not implement the IObj
clojure interface.
This commit is contained in:
Andrey Antukh 2022-09-26 23:59:20 +02:00
parent 47363d96f1
commit 2753a934aa
3 changed files with 23 additions and 5 deletions

View file

@ -31,9 +31,10 @@
(defn- handle-response-transformation (defn- handle-response-transformation
[response request mdata] [response request mdata]
(if-let [transform-fn (:transform-response mdata)] (let [response (if (sv/wrapped? response) @response response)]
(p/do (transform-fn request response)) (if-let [transform-fn (:transform-response mdata)]
(p/resolved response))) (p/do (transform-fn request response))
(p/resolved response))))
(defn- handle-before-comple-hook (defn- handle-before-comple-hook
[response mdata] [response mdata]

View file

@ -11,6 +11,20 @@
[app.common.data :as d] [app.common.data :as d]
[cuerdas.core :as str])) [cuerdas.core :as str]))
(defrecord WrappedValue [obj]
clojure.lang.IDeref
(deref [_] obj))
(defn wrap
([]
(WrappedValue. nil))
([o]
(WrappedValue. o)))
(defn wrapped?
[o]
(instance? WrappedValue o))
(defmacro defmethod (defmacro defmethod
[sname & body] [sname & body]
(let [[docs body] (if (string? (first body)) (let [[docs body] (if (string? (first body))

View file

@ -23,6 +23,7 @@
[app.rpc.mutations.projects :as projects] [app.rpc.mutations.projects :as projects]
[app.rpc.mutations.teams :as teams] [app.rpc.mutations.teams :as teams]
[app.util.blob :as blob] [app.util.blob :as blob]
[app.util.services :as sv]
[app.util.time :as dt] [app.util.time :as dt]
[clojure.java.io :as io] [clojure.java.io :as io]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
@ -278,8 +279,10 @@
(defmacro try-on! (defmacro try-on!
[expr] [expr]
`(try `(try
{:error nil (let [result# (deref ~expr)
:result (deref ~expr)} result# (cond-> result# (sv/wrapped? result#) deref)]
{:error nil
:result result#})
(catch Exception e# (catch Exception e#
{:error (handle-error e#) {:error (handle-error e#)
:result nil}))) :result nil})))