Log caught errors instead of rethrowing

This commit is contained in:
Dan Holmsand 2015-09-13 14:25:17 +02:00
parent 04daf6ac57
commit e12716da5d
3 changed files with 16 additions and 11 deletions

View File

@ -7,6 +7,7 @@
(declare ^:dynamic *ratom-context*)
(defonce ^boolean debug false)
(defonce ^boolean silent false)
(defonce -running (clojure.core/atom 0))
@ -206,7 +207,6 @@
(def ^:const clean 0)
(def ^:const maybe-dirty 1)
(def ^:const dirty 2)
(def ^:const failed 3)
(deftype Reaction [f ^:mutable state ^:mutable ^number dirtyness
^:mutable watching ^:mutable watches
@ -304,8 +304,10 @@
(ar parent)
(run parent))
(catch :default e
(set! (.-dirtyness parent) failed)
(set! (.-state parent) e)
;; Just log error: it will most likely pop up again at deref time.
(when-not silent
(js/console.error "Error in reaction:" e))
(set! (.-dirtyness parent) dirty)
(set! dirtyness dirty))))
IRunnable
@ -328,11 +330,6 @@
IDeref
(-deref [this]
(-check-clean this)
(when (== dirtyness failed)
(let [e state]
(set! dirtyness dirty)
(set! state nil)
(throw e)))
(if (and (nil? auto-run) (nil? *ratom-context*))
(when-not (== dirtyness clean)
(let [oldstate state
@ -482,7 +479,7 @@
(util/partial-ifn. callback-fn args nil)
false nil))
(comment
(do
(def perf-check 0)
(defn ratom-perf []
(dbg "ratom-perf")

View File

@ -254,10 +254,14 @@
b (reaction (if @a (throw (js/Error. "fail"))))
c (run! (try @b (catch :default e
(swap! catch-count inc))))]
(set! rv/silent true)
(is (= @catch-count 0))
(reset! a false)
(is (= @catch-count 0))
(reset! a true)
(is (= @catch-count 1))
(reset! a false)
(is (= @catch-count 1))))
(is (= @catch-count 1))
(set! rv/silent false)
(dispose c)
(is (= runs (running)))))

View File

@ -278,6 +278,7 @@
b (reaction (if @a (throw (js/Error. "reaction fail"))))
c (ar (fn [] (try @b (catch js/Object e
(swap! catch-count inc)))))]
(set! rv/silent true)
(is (= @catch-count 0))
(reset! a false)
(sync)
@ -285,4 +286,7 @@
(reset! a true)
(is (= @catch-count 0))
(sync)
(is (= @catch-count 1))))
(is (= @catch-count 1))
(set! rv/silent false)
(dispose c)
(is (= runs (running)))))