Clojurescript/Reagent 处理错误
Clojurescript/Reagent handling errors
我们开始将 Clojuescript/Reagent 用于我们产品的下一个开发阶段。基本上我们需要一种方法,在原子上做一个单一的操作,我们想出了一个方法,比如:
(def app-state (r/atom {}))
(defn select [index]
(swap! app-state
#(-> %
(assoc-in [:storyboard :pages 0 :layers 0 :children 0 :is_selected] true)
(assoc-in (GET ....)) ;; ajax call
(assoc-in [:selected :selected_tab] "tab_properties")
(assoc-in [:selected :page_object_cursor] [0])
)))
基本上交换得到一个函数,其中所有需要的操作都被链接起来。太棒了。但是,我正在寻找处理错误的方法。我想有一个单独的错误原子:
(def errors (r/atom []))
并且当发生错误时,捕获它并将其添加到错误中,而不交换应用程序状态(应用程序保持在最后的稳定状态)。所以我们使用 throw from the chain 和一个 try cactch
(defn change-title [title]
(try
(swap! app-state
#(-> %
(assoc :text title)
((fn [state]
(throw "there is an error")))
))
(catch js/Object e
(swap! errors conj e))))
所以我希望错误被捕获,并且@errors 拥有它。但现实是:
*未捕获有错误
weather_app$core$change_title @core.cljs?rel=1450266738018:59(匿名函数)@core.cljs?rel=1450266738018:108executeDispatch @react-with -addons.inc.js:3311SimpleEventPlugin.executeDispatch @react-with-addons.inc.js:17428forEachEventDispatch @react-addons.inc.js:3299executeDispatchesInOrder @react-addons.inc.js:3320executeDispatchesAndRelease @react -with-addons.inc.js:2693forEachAccumulated @react-with-addons.inc.js:19430EventPluginHub.processEventQueue @react-with-addons.inc.js:2900runEventQueueInBatch @react-addons.inc.js:11217ReactEventEmitterMixin.handleTopLevel @react-with-addons.inc.js:11243handleTopLevelImpl @react-with-addons.inc.js:11329Mixin.perform @react-with-addons.inc.js:18402ReactDefaultBatchingStrategy.batchedUpdates @react -with-addons.inc.js:9669batchedUpdates @react-with-addons.inc.js:16633ReactEventListener.dispatchEvent @react-with-addons.inc.js:11423*
(catch :default e
(swap! errors conj e))))
因为抛出的字符串不是对象,而是字符串!
我们开始将 Clojuescript/Reagent 用于我们产品的下一个开发阶段。基本上我们需要一种方法,在原子上做一个单一的操作,我们想出了一个方法,比如: (def app-state (r/atom {}))
(defn select [index]
(swap! app-state
#(-> %
(assoc-in [:storyboard :pages 0 :layers 0 :children 0 :is_selected] true)
(assoc-in (GET ....)) ;; ajax call
(assoc-in [:selected :selected_tab] "tab_properties")
(assoc-in [:selected :page_object_cursor] [0])
)))
基本上交换得到一个函数,其中所有需要的操作都被链接起来。太棒了。但是,我正在寻找处理错误的方法。我想有一个单独的错误原子:
(def errors (r/atom []))
并且当发生错误时,捕获它并将其添加到错误中,而不交换应用程序状态(应用程序保持在最后的稳定状态)。所以我们使用 throw from the chain 和一个 try cactch
(defn change-title [title]
(try
(swap! app-state
#(-> %
(assoc :text title)
((fn [state]
(throw "there is an error")))
))
(catch js/Object e
(swap! errors conj e))))
所以我希望错误被捕获,并且@errors 拥有它。但现实是:
*未捕获有错误
weather_app$core$change_title @core.cljs?rel=1450266738018:59(匿名函数)@core.cljs?rel=1450266738018:108executeDispatch @react-with -addons.inc.js:3311SimpleEventPlugin.executeDispatch @react-with-addons.inc.js:17428forEachEventDispatch @react-addons.inc.js:3299executeDispatchesInOrder @react-addons.inc.js:3320executeDispatchesAndRelease @react -with-addons.inc.js:2693forEachAccumulated @react-with-addons.inc.js:19430EventPluginHub.processEventQueue @react-with-addons.inc.js:2900runEventQueueInBatch @react-addons.inc.js:11217ReactEventEmitterMixin.handleTopLevel @react-with-addons.inc.js:11243handleTopLevelImpl @react-with-addons.inc.js:11329Mixin.perform @react-with-addons.inc.js:18402ReactDefaultBatchingStrategy.batchedUpdates @react -with-addons.inc.js:9669batchedUpdates @react-with-addons.inc.js:16633ReactEventListener.dispatchEvent @react-with-addons.inc.js:11423*
(catch :default e
(swap! errors conj e))))
因为抛出的字符串不是对象,而是字符串!