Skip to content

Instantly share code, notes, and snippets.

@mszajna
Created September 26, 2018 00:22
Show Gist options
  • Save mszajna/b998e829c19bd6205d4477b8bafbc713 to your computer and use it in GitHub Desktop.
Save mszajna/b998e829c19bd6205d4477b8bafbc713 to your computer and use it in GitHub Desktop.
(defmacro either-try
([body] `(try [~body] (catch Throwable e# [nil e#])))
([body pred]
`(try [~body] (catch Throwable e# (if (~pred e#) [nil e#] (throw e#))))))
(defn risky-op [] (throw (ex-info "Oops" {:error-type :my-specific-error})))
(let [[result error] (either-try (risky-op) #(-> % ex-data :error-type (= :my-specific-error)))]
(if error
(println "got" error)
(println result)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment