Safer fn calls

This commit is contained in:
Julien Eluard 2019-04-04 17:08:38 +02:00
parent a42b65ec07
commit 6c1bf3b4e2
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
2 changed files with 14 additions and 3 deletions

View File

@ -34,7 +34,7 @@
(= v 'properties) (get env :pluto.reader/properties)
(symbol? v) (get env v)
(query? v)
(when query-fn
(when (fn? query-fn)
(when-let [signal (query-fn ctx (substitute-query-values ctx env v))]
(let [o @signal]
(log/fire! ctx ::log/trace :query/resolve {:key v :value o})

View File

@ -35,15 +35,26 @@
(types/resolve ctx ext type arguments)
{:errors [(error/syntax ::error/invalid {:type :reference} {:type :event :data event})]}))
(defn- dispatch-event
"Safely call `event-fn`"
[event-fn ctx events]
(try
(when-let [o (event-fn ctx events)]
(log/fire! ctx ::log/error :event/dispatch {:reason :return-value-ignored :data o}))
(catch #?(:clj Exception :cljs :default) ex
(log/fire! ctx ::log/error :event/dispatch {:reason :exception-thrown :data ex}))))
(defn- dispatch-events
"Dispatches an event using ctx"
[{:keys [event-fn] :as ctx} events raw?]
(if (seq events)
(do
(log/fire! ctx ::log/trace :event/dispatch events)
(log/fire! ctx ::log/trace :event/dispatch {:data events})
(cond
raw? events
event-fn (event-fn ctx events)))
(fn? event-fn) (dispatch-event event-fn ctx events)
:else
(log/fire! ctx ::log/error :event/dispatch {:reason (if event-fn :invalid-event-fn :missing-event-fn) :data events})))
(log/fire! ctx ::log/error :event/dispatch {})))
(defn- resolve-event