Skip to content

Commit 57e9e62

Browse files
committed
Improve portal.runtime async error handling
1 parent f206eb6 commit 57e9e62

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

.clj-kondo/config.edn

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
:lint-as
66
{portal.async/let clojure.core/let
77
portal.sync/let clojure.core/let
8+
portal.async/try clojure.core/try
9+
portal.sync/try clojure.core/try
810
reagent.core/with-let clojure.core/let
911
portal.bench/simple-benchmark clojure.core/let
1012
portal.runtime.macros/extend-type? clojure.core/extend-type}}

src/portal/async.cljc

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
(ns ^:no-doc portal.async
2-
(:refer-clojure :exclude [let promise])
2+
(:refer-clojure :exclude [let try promise])
33
#?(:portal (:import) :cljs (:require-macros portal.async)))
44

55
(defmacro do [& body]
66
(reduce
77
(fn [chain form]
88
`(.then ~chain
9-
(fn [] (js/Promise.resolve ~form))))
10-
`(js/Promise.resolve nil)
9+
(fn [] (.resolve js/Promise ~form))))
10+
`(.resolve js/Promise nil)
1111
body))
1212

1313
(defmacro let [bindings & body]
1414
(->> (partition-all 2 bindings)
1515
reverse
1616
(reduce (fn [body [n v]]
17-
`(.then (js/Promise.resolve ~v)
17+
`(.then (.resolve js/Promise ~v)
1818
(fn [~n] ~body)))
1919
`(portal.async/do ~@body))))
2020

21+
(defmacro try [& body]
22+
(reduce
23+
(fn [chain form]
24+
(cond
25+
(and (coll? form) (= 'finally (first form)))
26+
`(.finally ~chain
27+
(fn []
28+
(portal.async/do ~@(rest form))))
29+
30+
(and (coll? form) (= 'catch (first form)))
31+
(clojure.core/let [[_ _type ex-binding & body] form]
32+
`(.catch ~chain
33+
(fn [~ex-binding]
34+
(portal.async/do ~@body))))
35+
36+
:else
37+
`(.then ~chain
38+
(fn []
39+
(.resolve js/Promise ~form)))))
40+
`(.resolve js/Promise nil)
41+
body))

src/portal/runtime.cljc

+7-6
Original file line numberDiff line numberDiff line change
@@ -380,21 +380,22 @@
380380

381381
(defn invoke [{:keys [f args]} done]
382382
(let [f (if (symbol? f) (get-function f) f)]
383-
(try
383+
(a/try
384384
(a/let [return (apply f args)]
385385
(done (assoc (source-info f) :return return)))
386386
(catch #?(:clj Exception :cljr Exception :cljs js/Error) e
387387
(done (assoc
388388
(source-info f)
389389
:error
390390
(-> (ex-info
391-
"invoke exception"
392-
{:function f
393-
:args args
394-
:found? (boolean (get-function f))}
391+
(ex-message e)
392+
{::function f
393+
::args args
394+
::found? (some? f)
395+
::data (ex-data e)}
395396
e)
396397
datafy
397-
(assoc :runtime #?(:bb :bb :clj :clj :cljs :cljs :cljr :cljr)))))))))
398+
(assoc :runtime (runtime)))))))))
398399

399400
(def ops {:portal.rpc/invoke #'invoke})
400401

src/portal/sync.cljc

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(ns ^:no-doc portal.sync
2-
(:refer-clojure :exclude [let]))
2+
(:refer-clojure :exclude [let try]))
33

44
(defmacro let [bindings & body]
55
`(clojure.core/let ~bindings ~@body))
6+
7+
(defmacro try [& body] `(~'try ~@body))

0 commit comments

Comments
 (0)