Skip to content

Instantly share code, notes, and snippets.

@AlexBaranosky
Last active October 1, 2015 22:15
Show Gist options
  • Save AlexBaranosky/546568f2b564d51e21e5 to your computer and use it in GitHub Desktop.
Save AlexBaranosky/546568f2b564d51e21e5 to your computer and use it in GitHub Desktop.
;; 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