Skip to content

Instantly share code, notes, and snippets.

@tmoerman
Last active August 29, 2015 14:01
Show Gist options
  • Save tmoerman/d7035032a1a1f4b15a6c to your computer and use it in GitHub Desktop.
Save tmoerman/d7035032a1a1f4b15a6c to your computer and use it in GitHub Desktop.
Clojure core.async exception handling
(defn nil->keyword [x] (if (nil? x) :nil x))
(defn nil-safe [f] (comp nil->keyword f))
(defn lift
[f]
(fn [x]
(cond
(instance? Throwable x) x
(nil? x) :nil
(= :nil x) :nil
:default (try ((nil-safe f) x)
(catch Throwable t t)))))
(defn log-throwable [t] (if (instance? Throwable t) (prn "got: " t)) t)
(def input (chan))
(->> input
(async/map< (lift #(/ 5 %)))
(async/map< (lift prn))
(async/map< log-throwable)
(suck))
(async/put! input 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment