File tree Expand file tree Collapse file tree 3 files changed +42
-12
lines changed Expand file tree Collapse file tree 3 files changed +42
-12
lines changed Original file line number Diff line number Diff line change 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)
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 }))))
You can’t perform that action at this time.
0 commit comments