簡體   English   中英

Clojurescript /試劑處理錯誤

[英]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 []))

並在發生錯誤時捕獲它並將其添加到錯誤中,而無需交換應用程序狀態(應用程序保持在最后的穩定狀態)。 因此,我們使用鏈條上的擲球和嘗試用仙人掌

(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-with-addons.inc.js:3299executeDispatchesInOrder @ react-with-addons.inc.js:3320executeDispatchesAndRelease @ react-with-addons.inc.js:2693forEachAccumulated @ react-with-addons.inc。 js:19430EventPluginHub.processEventQueue @ react-with-addons.inc.js:2900runEventQueueInBatch @ react-with-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-與-addons.inc.js:11423 *

(catch :default e
  (swap! errors conj e))))

因為拋出的字符串不是對象,而是字符串!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM