Search code examples
error-handlingclojurescriptomreagent

Clojurescript/Reagent handling errors


We are starting to use Clojuescript/Reagent for our next development phase of our product. Basically we need a way, to do a single operation on the atom and we came up with a method like: (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])
         )))

Basically the swap gets a function where all needed operations are chained. It is brilliant. However, I am looking at ways to handle errors. I would like to have a separate atom for errors:

(def errors (r/atom []))

and when an error occurs, to capture it and add it to the errors, without having the app-state swapped (the application remains in the last stable state). So we use throw from the chain and a 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))))

So I would expect the error to be caught, and @errors to have it. But the reality is:

*Uncaught there is an error

weather_app$core$change_title @ core.cljs?rel=1450266738018:59(anonymous function) @ 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-with-addons.inc.js:11423*


Solution

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

    because the string thrown is not an object, but a string!