Clojure/Emacs/nrepl: Stacktrace-less error messages
Ever since I started using the Emacs + nrepl combination to play around with Clojure I’ve been getting fairly non descript error messages whenever I pass the wrong parameters to a function.
For example if I try to update a non existent key in a form I get a Null Pointer Exception:
> (update-in {} [:mark] inc)
NullPointerException clojure.lang.Numbers.ops (Numbers.java:942)
In this case it’s clear that the hash doesn’t have a key ':mark' so the function blows up. However, sometimes the functions are more complicated and this type of reduced stack trace isn’t very helpful for working out where the problem lies.
I eventually came across a thread in the nrepl-el forum where Tim King suggested that adding the following lines to the Emacs configuration file should sort things out:
~/.emacs.d/init.el
(setq nrepl-popup-stacktraces nil)
(setq nrepl-popup-stacktraces-in-repl t)
I added those two lines, restarted Emacs and after calling the function again got a much more detailed stack trace:
> (update-in {} [:mark] inc)
java.lang.NullPointerException:
Numbers.java:942 clojure.lang.Numbers.ops
Numbers.java:110 clojure.lang.Numbers.inc
core.clj:863 clojure.core/inc
AFn.java:161 clojure.lang.AFn.applyToHelper
AFn.java:151 clojure.lang.AFn.applyTo
core.clj:603 clojure.core/apply
core.clj:5472 clojure.core/update-in
RestFn.java:445 clojure.lang.RestFn.invoke
NO_SOURCE_FILE:1 user/eval9
...
From reading this stack trace we learn that the problem happens when the inc function is called with a parameter of 'nil'. We’d see the same thing if we called it directly:
> (inc nil)
java.lang.NullPointerException:
Numbers.java:942 clojure.lang.Numbers.ops
Numbers.java:110 clojure.lang.Numbers.inc
NO_SOURCE_FILE:1 user/eval14
...
Although Clojure error messages do baffle me at times, I hope things will be better now that I’ll be able to see on which line the error occurred.
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.