Skip to content

Instantly share code, notes, and snippets.

@aboekhoff
Created February 11, 2010 12:16
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 aboekhoff/301463 to your computer and use it in GitHub Desktop.
Save aboekhoff/301463 to your computer and use it in GitHub Desktop.
(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