Created
September 26, 2018 00:22
-
-
Save mszajna/b998e829c19bd6205d4477b8bafbc713 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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