Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created December 22, 2010 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiredman/750897 to your computer and use it in GitHub Desktop.
Save hiredman/750897 to your computer and use it in GitHub Desktop.
(def ^{:dynamic true} *handler* nil)
(defn error-point [fun]
(let [[result exception] (binding [*handler* *handler*]
(try
[(fun) nil]
(catch Throwable t
(if *handler*
[nil (partial *handler* t)]
(throw t)))))]
(if exception
(recur exception)
result)))
(defn handle-errors [fun handler]
(let [former-handler *handler*]
(set! *handler* (fn [x]
(set! *handler* former-handler)
(handler x)))
(let [x (fun)]
(set! *handler* former-handler)
x)))
(defmacro with-errors [& body]
`(error-point (fn [] ~@body)))
(defmacro with-errror-continue [fun & body]
`(handle-errors (fn [] ~@body) ~fun))
(defn set-error-continue! [handler]
(let [former-handler *handler*]
(set! *handler* (fn [x]
(set! *handler* former-handler)
(handler x)))))
(comment
(with-errors
(with-errror-continue (fn [_] (println "you have an error"))
(+ 1 \a)))
(with-errors
(set-error-continue! (fn [_] (println "ERROR!")))
(+ 1 \a))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment