Skip to content

Commit 3e37a8a

Browse files
committed
add working version of with-try macro with some initial tests
1 parent b29857c commit 3e37a8a

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

src/nodely/api/v0.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
nodely.data/leaf
2121
nodely.data/sequence
2222
nodely.data/branch
23+
nodely.data/with-try
2324
engine-core/checked-env)
2425

2526
(import-fn nodely.engine.lazy/eval-node-with-values eval-node-with-values)

src/nodely/data.clj

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,36 @@
198198
(do (assert (= catch 'catch))
199199
[t s expr]))]))
200200

201+
(defn tuple-to-handler
202+
[m]
203+
(fn [error]
204+
(if-let [f (some (fn [[ex-class handler]] (when (instance? ex-class error) handler)) m)]
205+
(f error)
206+
(throw error))))
207+
201208
(defn with-try-expr
202209
[clauses]
203-
(let [clauses (into {} (for [[c t s expr] clauses]
210+
(let [clauses (into [] (for [[c t s expr] clauses]
204211
(do (assert (= c 'catch))
205-
[(resolve t) (eval `(fn [~s] ~expr))]
206-
#_[t s expr])))]
212+
(if-let [t (resolve t)]
213+
[t (eval `(fn [~s] ~expr))]
214+
(throw (ex-info (str "Could not resolve exception class: " t) {:type t}))))))]
207215
clauses))
208216

209217
(defmacro with-try
210218
[env & body]
211219
`(with-error-handler
212220
~env
213-
~(with-try-expr body)))
221+
(tuple-to-handler ~(with-try-expr body))))
214222

215223
(comment
216-
(macroexpand-1 '(with-try {:a "hi"}
224+
(macroexpand-1 '(with-try {:a (leaf [:x] (comp inc :x))}
217225
(catch Exception e (println e))
218226
(catch Throwable t (println t))))
227+
228+
(tuple-to-handler {java.lang.Exception identity,
229+
java.lang.Throwable identity})
230+
219231
;
220232
)
221233

test/nodely/api_test.clj

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,27 @@
303303
remove-keys)]
304304
(update-node-engine-test-suite engine))))
305305

306-
(t/deftest try-env
307-
(t/testing "try-env works"
308-
(t/matching 3
309-
(api/eval-key
310-
(api/try-env exceptions-all-the-way-down
311-
(catch Throwable _ 0))
312-
:d {::api/engine :sync.lazy}))))
306+
(t/deftest with-try
307+
(t/testing "with-try works Throwable catches first"
308+
(t/matching "Could not resolve exception class: NonSenseException"
309+
(try
310+
(eval '(api/with-try exceptions-all-the-way-down
311+
(catch NonSenseException _ 0)))
312+
(catch clojure.lang.Compiler$CompilerException e
313+
(ex-message (.getCause e))))))
314+
315+
(t/testing "with-try works Throwable catches first"
316+
(t/matching 0
317+
(api/eval-key
318+
(api/with-try exceptions-all-the-way-down
319+
(catch Throwable _ -3)
320+
(catch clojure.lang.ExceptionInfo _ 0))
321+
:d {::api/engine :sync.lazy})))
322+
323+
(t/testing "with-try works ExceptionInfo catches first"
324+
(t/matching 0
325+
(api/eval-key
326+
(api/with-try exceptions-all-the-way-down
327+
(catch clojure.lang.ExceptionInfo _ -3)
328+
(catch Throwable _ 0))
329+
:d {::api/engine :sync.lazy}))))

0 commit comments

Comments
 (0)