Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Clojure/Emacs/nrepl: Stacktrace-less Error Messages

DZone's Guide to

Clojure/Emacs/nrepl: Stacktrace-less Error Messages

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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.

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat

Topics:

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}