Created
February 11, 2010 12:16
-
-
Save aboekhoff/301463 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns debuggery | |
(:use clojure.walk | |
clojure.stacktrace | |
clojure.contrib.macro-utils | |
clojure.contrib.pprint)) | |
(comment | |
(with-debuggery | |
(defn bad-plus [n] (+ n nil)) | |
(defn use-bad-plus [] (bad-plus 42))) | |
(use-bad-plus) | |
"origin: | |
(fn* bad-plus ([n] (+ n nil))) | |
trace: | |
java.lang.NullPointerException: null | |
at origin: | |
(fn* use-bad-plus ([] (bad-plus 42))) | |
trace: | |
java.lang.NullPointerException: null | |
at clojure.lang.Reflector.invokeNoArgInstanceMember (Reflector.java:263)" | |
) | |
(defn report-error [{:keys [origin error]}] | |
(let [t (Thread/currentThread)] | |
(println "origin:") | |
(println origin) | |
(println) | |
(println "trace:") | |
(print-cause-trace (root-cause error) 1) | |
(.stop t))) | |
(defn exception-wrap [body] | |
`(fn [& xs#] | |
(try (apply ~body xs#) | |
(catch Exception e# | |
(report-error | |
{:origin (quote ~body) | |
:error e#}))))) | |
(defn maybe-wrap [x] | |
(if (not (and (seq? x) (= 'fn* (first x)))) x | |
(exception-wrap x))) | |
(defmacro with-debuggery [& forms] | |
`(do ~@(postwalk maybe-wrap (mexpand-all forms)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment