Debugging a Clojure Exception in the Lein REPL


See the exception stacktrace in the Leiningen REPL. When an exception is thrown in the REPL, it is saved to the variable *e. To see it, you can use the native Java command to print the stacktrace-

(.printStackTrace *e)

Picked this up from p.63 of Joy of Clojure. After a bit more research (Googling, that is), Clojure has a couple other methods that help debugging.

First, you need to require the stacktrace namespace (see here for the right way to require namespaces):

(use 'clojure.stacktrace)

To see the root-cause, you can observe that in a nice map which gives :cause, :via, and :trace:

(root-cause *e)

For a full stacktrace, use the Clojure print method which defaults to unlimited lines shown (pass # of lines as 2nd argument to limit):

(print-stack-trace *e)

The shortest debugging method (which is an alias of the two methods above (print-stack-trace (root-cause *e) 8))) gives you the exception plus 8 lines:

(e)

You can see the source code here