Last active
October 1, 2015 22:15
-
-
Save CampingScorpion/546568f2b564d51e21e5 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
;; ALERT ALERT -- don't use this, it's busted code! | |
(defn catch-form? [form] | |
(and (list? form) | |
(= 'catch (first form)))) | |
(defn transform-catch-form [catch-form] | |
(if-not (sequential? (second catch-form)) | |
catch-form | |
(let [[_catch [k v] e & catch-body] catch-form] | |
`(catch Throwable ~e | |
(when (= ~v (get (ex-data ~e) ~k)) | |
~@catch-body))))) | |
(defmacro try++ [& args] | |
(let [body (remove catch-form? args) | |
catch-forms (filter catch-form? args)] | |
`(try | |
~@body | |
~@(map transform-catch-form catch-forms)))) | |
;; example: | |
(try++ | |
(throw (ex-info "asdasd" {:status 400})) | |
(throw (Exception. "Boom")) | |
(catch [:status 400] e2 | |
(println (ex-data e2))) | |
(catch Exception e1 | |
(println (.getMessage e1)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment